Click here to Skip to main content
15,885,757 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 146.7K   1.6K   47  
This article talks about the powerful SharePoint 2010 Client Object Model Capabilities and various ways to utilize it.
<%@ Assembly Name="ClientOMUIActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fa0e8dd370ad1af3" %>
<%@ 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"> 
  var messageId;
    function BtnCreateDocLibCancel_Click() { 
       SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 'Cancelled clicked'); 
    } 

    function BtnCreateDocLibOk_Click() { 
        var form = document.forms.<%SPHttpUtility.NoEncode(Form.ClientID,Response.Output);%>; 
        var ListNameUrl = form.<%SPHttpUtility.NoEncode(TxtListName1.ClientID,Response.Output);%>.value; 
        
        //Create list using client OM 
        createDocLib(ListNameUrl);

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

    //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 listCreationInfo = new SP.ListCreationInformation(); 
    listCreationInfo.set_title(listName); 
    listCreationInfo.set_templateType(SP.ListTemplateType.documentLibrary); 
    listCreationInfo.set_quickLaunchOption(SP.QuickLaunchOptions.on); 

    this.oList = oWebsite.get_lists().add(listCreationInfo); 
    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>    
      <SharePoint:FormDigest ID="FormDigest1" runat="server" />
      <wssuc:InputFormSection Title="Document Library name" runat="server"> 
        <Template_Description> 
          <SharePoint:EncodedLiteral ID="EncodedLiteral111" runat="server" text="Define document library name to be created." EncodeMethod='HtmlEncode'/> 
        </Template_Description> 
        <Template_InputFormControls> 
          <wssuc:InputFormControl runat="server"> 
            <Template_Control> 
              <table border="0" cellspacing="1"> 
                <tr> 
                  <td class="ms-authoringcontrols" colspan="2" nowrap="nowrap"> 
                    <SharePoint:EncodedLiteral ID="EncodedLiteral777" runat="server" text="Document library name will be used as url and as the title" EncodeMethod='HtmlEncode'/>: 
                    <font size="3">&#160;</font><br /> 
                  </td> 
                </tr> 
                <tr> 
                  <td dir="ltr"> 
                  <table> 
                      <tr> 
                      <td>
                          <tdnowrap="nowrap"colspan="2"class="ms-authoringcontrols"> 
                            <wssawc:InputFormTextBox title="Enter document library name" class="ms-input" ID="TxtListName1" Columns="35" Runat="server" maxlength="255" size="60" width="100%" /> 
                          </td> 
                      </tr> 
                  </table> 
                  </td> 
                </tr> 
              </table> 
            </Template_Control> 
          </wssuc:InputFormControl> 
        </Template_InputFormControls> 
      </wssuc:InputFormSection> 
      <wssuc:ButtonSection runat="server" ShowStandardCancelButton="False"> 
        <Template_Buttons> 
          <asp:placeholder ID="Placeholder111" runat="server"> 
          <input class="ms-ButtonHeightWidth" type="button" name="BtnOk" id="Button1" value="OK" onclick="BtnCreateDocLibOk_Click()"  /> 
            <SeparatorHtml> 
                <span id="idSpace" class="ms-SpaceBetButtons" /> 
            </SeparatorHtml> 
          <input class="ms-ButtonHeightWidth" type="button" name="BtnCancel" id="Button2" value="Cancel" onclick="BtnCreateDocLibCancel_Click()" />            
          </asp:PlaceHolder> 
        </Template_Buttons> 
      </wssuc:ButtonSection> 
      </td>
    </tr>
    </table> 
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
:::Create a SharePoint 2010 document library using ECMASCRIPT Client Object Model:::
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
:::Create a SharePoint 2010 document library using ECMASCRIPT 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