Thursday, March 3, 2011

How to get list items through Sharepoint Object Model

Programmatically we can get sharepoint list items through Object Model.
It is so easy.

Just we need SPSite, SPWeb instances and get the list out of it. Then we need to formulate SPQuery and get the items.

There you go.

using(SPSite site=new SPSite("Site_URL")
{
   using(SPWeb web=site.OpenWeb())
   {
    SPList list = web.Lists["List_Name"];
    SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"Recursive\"";// this will search your folders too
    SPListCollection newsListColl = list.GetItems(query);
    }
}

Friday, February 25, 2011

How to update User profile property using Object Model

User profiles are just repositories for information about individual people. We can programmatically update the properties of user profiles. You just need to specify the account you want to update the property.

Here is the fun part: the code :)

using(SPSite site = new SPSite(http://Site_URL/))
{
     using(SPWeb web = site.OpenWeb())
       {
            ServerContext context = ServerContext.GetContext(site);
         UserProfileManager profMgr = new UserProfileManager(context);
         string acc = @"domaniname\loginname";
         UserProfile profile = profMgr.GetUserProfile(acc);
         profile["AboutMe"].Value = (String.IsNullOrEmpty(txtPropValue.Text)) ? "property_value" : txtPropValue.Text;
         profile.Commit();
      }
}



Happy Coding!!!

Friday, February 18, 2011

How to reorder columns in Sharepoint Lists through Webservice


We have already seen how to reorder list columns through OM in my earlier post.

Let’s see how to achieve it through web service.

We are going to order the fields in the site content type that has been associated in the list through web service. The field ordering is done by the order in which <field> are presents within <Fields> tag. Hence we are going to reorder the fields by deleting the field tag and then adding them in the order we want. 

As we are going to make change in site content type we will use webs.asmx.
The following example will use UpdateContentType method of the webs.asmx to add and delete the fields in the content type.

Xml file

<?xml version="1.0" encoding="utf-8" ?>
<soapEnvelope>
  <soapBody>
    <UpdateContentType>
      <contentTypeId>0x010039AAF2FFAC8C2E468531F5B23B4595F2</contentTypeId>
      <contentTypeProperties>
        <ContentType Group="MyCT"/>
      </contentTypeProperties>
      <newFields>
        <Fields>
          <Method ID="1">
            <Field ID="{9fb22ff8-a1e6-46e5-9ee1-bcc1c95fab35}" Name="ColumeOrgFirst"/>
          </Method>
          <Method ID="2">
            <Field ID="{082f8d40-6f6b-438d-96dd-4a9958c08d8c}" Name="ColumeOrg2"/>
          </Method>
        </Fields>
      </newFields>

      <updateFields>
        <Fields>
        </Fields>
      </updateFields>

      <deleteFields>
        <Fields>
          <Method ID="1">
            <Field ID="{9fb22ff8-a1e6-46e5-9ee1-bcc1c95fab35}" Name="ColumeOrgFirst"/>
          </Method>
          <Method ID="2">
            <Field ID="{082f8d40-6f6b-438d-96dd-4a9958c08d8c}" Name="ColumeOrg2"/>
          </Method>
        </Fields>
      </deleteFields>

    </UpdateContentType>
  </soapBody>
</soapEnvelope>


Code

namespace CTOrder
{
    class Program
    {
        static void Main(string[] args)
        {
            webWS.Webs myWebs = new webWS.Webs();
            myWebs.Credentials = System.Net.CredentialCache.DefaultCredentials;

            myWebs.Url = "http://Site_URL/_vti_bin/webs.asmx";
            XmlReader myXmlReader = new XmlTextReader(@"ct.xml");
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(myXmlReader);
            XmlNode myProps = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/contentTypeProperties");
            XmlNode myDel = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/deleteFields");
            XmlNode myUpdate = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/updateFields");
            XmlNode myNew = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/newFields");
            XmlNode res = myWebs.UpdateContentType("0x010039AAF2FFAC8C2E468531F5B23B4595F2", myProps, myNew, myUpdate, myDel);

            myXmlReader.Close();

            Console.WriteLine(res.InnerXml);
            Console.ReadLine();
        }
    }
}


Happy Coding!!!