Client Side Object Model in SharePoint 2010

SharePoint 2010 offers a useful way to access SharePoint objects from the client side. A developer can use .Net managed code, ECMAScript to access SharePoint objects like List, Site, Library etc.

On this blog, I will write a code to select data from SharePoint list. I will use .Net managed code to do this.


First of all, you need to add references of 2 dlls.

Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll which are available in hive(14)/ISAPI folder.

Once dlls got referenced in your project, you may start coding.

C# code to bind all Title to drop down from a list with name “MyList

private void BindData()
        {
            ClientContext context1 = new ClientContext(“siteurl”);
            context1.AuthenticationMode = ClientAuthenticationMode.Anonymous;

            List list1 = context1.Web.Lists.GetByTitle(“MyList”);
            CamlQuery query1 = new CamlQuery();
  
            query1.ViewXml = @”<Query></Query>”;

            Microsoft.SharePoint.Client.ListItemCollection listCollection1 = list1.GetItems(query1);
            context1.Load(listCollection1);
            context1.ExecuteQuery();
            DataTable dTable1 = new DataTable();
            dTable1.Columns.Add(“Title”);
             
            for (int iCntr1 = 0; iCntr1 < listCollection1.Count; iCntr1++)
            {

                dTable1.Rows.Add(dTable1.NewRow());
                if (listCollection1[iCntr1][“Title”] != null)
                {
                    dTable1.Rows[dTable1.Rows.Count – 1][“Title”] = listCollection1[iCntr1][“Title”].ToString();
                }
               
            }          
           
            if (dTable1.Rows.Count > 0)
            {
                dropdownlist1.DataValueField = “Title”;
                dropdownlist1.DataTextField = “Title”;
                dropdownlist1.DataSource = dTable1;
                dropdownlist1.DataBind();
                dropdownlist1.Items.Insert(0, “All”);
            }
        }


You may bind data with any data control, here I have used DropDown control.


JavaScript Object Model (JSOM)
ECMAScript is a java script based scripting language which is used in SharePoint to access objects client-side.
We can write ECMA Script in an aspx file and access SharePoint objects.

Below script will count the no. of items present in the list.

Create a list with name “MyList” in SharePoint.
Add few items.
Create a web part page and add content editor web part on that page.
Put below code in Editor and save the page.
Refresh your page, it will give you alert with a number of items available in mentioned list.

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(MainFunction, "SP.js");

var objContext = null;
var objWeb = null
var objList = null;
var objItem = null;

function MainFunction() {
objContext = new SP.ClientContext.get_current();
objWeb = objContext.get_web();
objList = objWeb.get_lists().getByTitle("MyList");

objContext.load(objList);

objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args) {
alert('Item Count: ' + objList.get_itemCount());
}

function onFail(sender, args) {
alert('Some error has occured.');
}
</script>

ExecuteOrDelayUntilScriptLoaded(MainFunction, “SP.js”) function takes the name of the function that needs to be executed as the main function and will load the SP.js file, which is the core of Client Object Model.


get_web() – Get the current site
getByTitle() – To get the list that is passed as a parameter
So by using ECMAScript no need to write any C# code to fetch list data.

Leave a Comment

RSS
YouTube
YouTube
Instagram