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!!!

How to reorder columns in Sharepoint Lists through Object model

Let's first see what are site columns, list columns and content types 

Site columns  
Site columns are those columns created at site level. These columns will be available across the site. We can add these columns to any list within the site. We can create the site columns by navigating to site columns gallery from site settings.

List columns
List columns are the columns to the list they are created. These columns cannot be added to other lists. If we want the same column to be added to some other list, we need to create a site column and then attached it to list or we need a new column to the list.

Content Types
Content types are nothing but group of columns. Content types created at site level are known as site content types. Content types created at list level are known as list content types.
Whenever we create a new Custom list, the content type “Item” gets attached to it. [For Doc library, content type “document” gets associated with it].

Whenever we add list columns to the list, those list columns will gets automatically associated with the existing content type(i.e.)”Item”. We might wonder that we did not explicitly create a content type, but how come it is getting associated. That’s the power of content type. You can see this behavior by enabling “Allow management of content type” in advanced settings of list settings. Then click on the item content type and have a look. Along with Title column you would be able to see the list columns associated with it. 


Now let’s see how we can re-order list columns through OM.
Upon knowing the fact that list columns gets automatically get associated with the content type, we are going to reorder the fields(columns) in the content type of the list. 

The code given below reorders the columns of a custom list.

Note: This changes the column order in new and edit form of the items in List. It does not change the order in display form. To achieve the column reordering in display form, you need to reorder columns in views.

using (SPSite site = new SPSite("http://SiteURL/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContentType ct = web.Lists["List_Name"].ContentTypes["Item"];
                    SPFieldLinkCollection flinks = ct.FieldLinks;

                    string[] fields = new string[3] { "firstCol", "Title", "secondCol" };
                    flinks.Reorder(fields);
                    ct.Update();
                }
            }