Click here to Skip to main content
15,896,154 members
Articles / Web Development / ASP.NET

Exploring SharePoint 2010 Client Object Model Capabilities

Rate me:
Please Sign up or sign in to vote.
4.54/5 (29 votes)
5 Jan 2011CPOL21 min read 147K   1.6K   47  
This article talks about the powerful SharePoint 2010 Client Object Model Capabilities and various ways to utilize it.
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="/_controltemplates/InputFormSection.ascx" %> 
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="/_controltemplates/InputFormControl.ascx" %> 
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="/_controltemplates/ButtonSection.ascx" %> 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListCreationPage.aspx.cs" Inherits="ClientOMUIActions.Layouts.ClientOMUIActions.ListCreationPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

<script type="text/javascript" src="/_layouts/SP.debug.js"> 
/// <reference name="MicrosoftAjax.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />
    var messageId;
   
    function BtnViewAllLists_Click() { 
        var form = document.forms.<%SPHttpUtility.NoEncode(Form.ClientID,Response.Output);%>; 
        var labelName = document.forms.<%lblLists.ClientID%>; 
        
        //Create list using client OM 
        showAllLists(labelName);

        //SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, ListNameUrl); 
    } 

    function showAllLists(labelName) {
    var clientContext = new SP.ClientContext(); 
    var oWebsite = clientContext.get_web(); 
    var listCollection = oWebsite.get_lists();
    }



    //Actual JS client side object model calls 
function createDocLib(listName) { 
    //Create client context. 
    var clientContext = new SP.ClientContext(); 
    var oWebsite = clientContext.get_web(); 

    //Let's create list creation information object 

    var docLibCreationInfo = oWebsite.
    var listCreationInfo = new SP.ListCreationInformation(); 
    listCreationInfo.set_title(listName); 
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements); 
    listCreationInfo.set_quickLaunchOption(SP.QuickLaunchOptions.on); 

    this.oList = oWebsite.get_lists().add(listCreationInfo); 

    //Let's create also new item to the list to be created 
    var itemCreateInfo = new SP.ListItemCreationInformation(); 
    this.oListItem = oList.addItem(itemCreateInfo); 
    oListItem.set_item('Title', 'Example item for ' + listName); 
    oListItem.set_item('Body', 'Hello seminar audience. From list ' + listName); 
    oListItem.update(); 

    clientContext.load(oListItem); 
    clientContext.load(oList); 

    //Execute the actual script 
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 
} 

//Called if client side OM is successful 
function onQuerySucceeded() { 
    //Remove the 'creating' event notification 
    if(messageId != null)
    {
        SP.UI.Notify.removeNotification(messageId); 
    }
    //Add 'created' notification as non sticky 
    messageId = SP.UI.Notify.addNotification("List <b>" + oList.get_title() + "</b> created...", false, "", null); 
} 

function onQueryFailed(sender, args) { 
    //Remove the 'creating' event notification 
    if(messageId != null)
    {
        SP.UI.Notify.removeNotification(messageId); 
    }
    //Shown in case of error on the JS OM call 
    messageId = SP.UI.Notify.addNotification("Operation was cancelled...", false, "", null); 
} 



</script>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table id="maintable" border="0" cellspacing="0" cellpadding="0" class="ms-propertysheet" width="100%"> 
    <tr>
    <td>    
      <wssuc:InputFormSection Title="List name" runat="server"> 
        <Template_Description> 
          <SharePoint:EncodedLiteral ID="EncodedLiteral11" runat="server" text="Define list name to be created." EncodeMethod='HtmlEncode'/> 
        </Template_Description>        
      </wssuc:InputFormSection> 
      <wssuc:ButtonSection runat="server" ShowStandardCancelButton="False"> 
        <Template_Buttons> 
          <asp:placeholder ID="Placeholder11" runat="server"> 
          <input class="ms-ButtonHeightWidth" type="button" name="BtnOk" id="Button11" value="View All Lists" onclick="BtnViewAllLists_Click()"  /> 
          <label class="ms-input" id="lblLists" runat="server" />          
          </asp:PlaceHolder> 
        </Template_Buttons> 
      </wssuc:ButtonSection> 
      </td>
    </tr>
    </table> 
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle1" runat="server">
:::View all SharePoint 2010 document library using Client Object Model:::
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea1" runat="server" >
:::View all SharePoint 2010 document library using Client Object Model:::
</asp:Content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
A SharePoint Kid playing in Kolkata, India.

Working with one of the leading software company and currently working with SharePoint technologies.

Enjoys Cricket, National & World Music.

Favourite band include Linkin Park, Beatles, Oasis, Match Box 20, Noori, Nirvana, Nickelback etc.

Comments and Discussions