Thursday, August 18, 2011

SharePoint 2010 Code Tips – Client Object Model -Add a Site Column to a List

Technorati Tags: ,,

I am going to start sharing more of the code I post on the MSDN forums. These postings will always begin with SharePoint 2010 Code Tips. I will do my best to explain what scenarios the tip may be useful for. With the advent of Office 365 there is a rising interest in accomplishing standard tasks remotely. For instance, adding a site column to an existing list. If you are developing a remote application for doing simple site administration this can be a common task. Many developers know how to accomplish this using the server object model, but many are not familiar on how to do it remotely. In this posting I will show how to accomplish this using the managed Client Object Model and also using both the Webs and Lists out of the box web services.

Add a Site Column with CSOM

The code below uses the managed CSOM to locate a site column and add it to an existing document library. It finds the site column by filtering the Site’s AvailableFields FieldCollection using a lambda query. The second step is to get the document library you want to add the column to by using the Web’s ListCollection GetListByTitle method. Next add the Field to the List’s Fields collection. Finally, update the List and call the ClientContext’s  ExecuteQuery method.

public static void AddSiteColumn(string siteColumnName)
{
    ClientContext context = new ClientContext("http://basesmc2008");
    Web site = context.Web;
    FieldCollection collFields = site.AvailableFields;       

    var siteColumn = context.LoadQuery(collFields.Where
        (c => c.Title == siteColumnName));

    context.ExecuteQuery();

    if (siteColumn != null && siteColumn.Count() == 1)
    {
        List list = context.Web.Lists.GetByTitle("tester2");
        list.Fields.Add(siteColumn.First());
        list.Update();
        context.ExecuteQuery();         
    }
}

 

Add a Site Column with Web Services

Those of us who have been developing on SharePoint since 2003 know how important using SharePoint web service can be. Even though the CSOM has made the coding simpler many tasks can still be done using out the box web services. The code sample below starts by using the Webs GetColumns method to return a list of site columns. Using xml linq you obtain the complete field node. Next add the field node to the list using the Lists UpdateList method. A required step to update the list is to get the current version of the list. The current version must be sent in the UpdateList  method.

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;


public static XmlNode UpdateListAddSiteColumn(string siteColumnName)
{

    XmlNode resultNode = null;

    webs.Webs webs = new webs.Webs();
    webs.Url = "http://basesmc2008/_vti_bin/webs.asmx";
    webs.UseDefaultCredentials = true;

    XmlNode columnsNode = webs.GetColumns();

    XElement xColumns = XElement.Parse(columnsNode.OuterXml);

    var siteColumn = from t in xColumns.Elements()
                     where t.Attribute("DisplayName").Value == siteColumnName
                                                    select t;

    if (siteColumn != null && siteColumn.Count() == 1)
    {
        string xml = "<Batch OnError='Continue'>";
        xml += "<Fields><Method ID='1' Cmd='New'>";
        xml += "!@siteColumnSchema";
        xml += "</Method></Fields></Batch>";

        xml = xml.Replace("!@siteColumnSchema", siteColumn.First().ToString());

        listservice.Lists listProxy = new listservice.Lists();

        listProxy.Url = "http://basesmc2008/_vti_bin/lists.asmx";
        listProxy.UseDefaultCredentials = true;

        XmlNode listNode = listProxy.GetList("tester2");
        XmlNode version = listNode.Attributes["Version"];

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        XmlNode newFieldsNode = doc.SelectSingleNode("//Fields");

        resultNode = listProxy.UpdateList("tester2", null, newFieldsNode, null, null, version.Value);
    }

    return resultNode;

}

 

As you can see the CSOM example is much easier than the web services example. Keep in mind that the adoption of SharePoint is now becoming so widely adopted, that many non-.Net developers are trying to integrate with it. Many of these developers are familiar with using web services, so the death of Web 2.0 web services in SharePoint is still a long way off.

No comments:

Post a Comment