Sunday, March 7, 2010

After I have written nice posts on complete details on Client Object Model and ECMAScript introduction planned to write introduction posts on Managed Client OM and Silverlight Client OM. So, in this post, we will discuss on Managed Client Object Model.

So, Managed Client Object Model is simple .NET code written based on CLR. So, you can use C#.net or VB.net to write code and execute whatever you want against SharePoint server.So, you can use any c# project to write code and run it. But, are there any prerequisites to write code? Do we need to follow some rules or process?

First, to write client side code, we need SharePoint libraries which has all the methods and classes to communicate with the SharePoint server. For this reason Microsoft SharePoint 2010 provides us the client side dll's to write Managed code. Below are the details.
DLL's needed: Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll. Find these files in the 14/ISAPI folder. Usually, the location would be at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI".

How to create a project to start working and communicate to SharePoint 2010 server? Follow the steps below.
  1. Open Visual Studio 2010.
  2. File -> New -> Project -> Visual C# -> Choose Console Application from the list of C# project types. [Make sure you have selected .NET Framework 3.5 on the top of window as shown in below.]

  3. Give some name to the project. I have given some valid name as "SP2010_HelloWorld".
  4. Now next step is getting the SharePoint Client Dll's reference to our project. So, for this get the SharePoint dll's to the client machine [Where we created project] and paste the DLL's in some safe location. I copied them to C:\SP2010_ClientDLL\.
  5. Now, go to Visual Studio 2010 project right click on project -> select References and browse to location where client dll's copied and select both dll's and hit ok.
  6. Finally, our references folder should looks like below.

  7. Now we are ready with all prerequisites and part left is writing code. I will show you simple code on how to write the code for getting web site title and description.
  8. Before start coding, we need add reference to the namespace in page by declaring using keyword as shown below.

    using Microsoft.SharePoint.Client;
    
  9. This is the code to get data from a SharePoint server, in this example we are retrieving Title and Description of a web site.

    static void Main(string[] args)
            {
                LoadSiteData();
            }
    
            private static void LoadSiteData()
            {
                string webUrl = "http://nb16";
                ClientContext context = new ClientContext(webUrl);
                Web web = context.Web;
                //Loads all web properties.
                context.Load(web);
                //Execute the query and load the object into the response given in load() method.
                context.ExecuteQuery();
                Console.WriteLine(web.Title);
                Console.ReadLine();
            }
    
    NOTE: Remember, you need to change the webUrl to actual SharePoint site url in the above example.
  10. Now run the code and see the output. You will see the title and description of the SharePoint web site.
That's it!!! Very simple and easier way. SharePoint 2010 client side development is very easy now. Lets rock it....

14 comments:

  1. I do not have
    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
    I'm running vs2010, but can't find references to the Microsoft.SharePoint.Client.
    What am I missing?
    I only have C:\Program Files (x86)\Common Files\microsoft shared\Web Server Extensions\12

    ReplyDelete
  2. Hi there,
    This is applicable to SharePoint 2010. And the dlls will be on the SharePoint server [Where SharePoint is installed.] You need to get them from that server and add them as references in your project. So, go to SharePoint 2010 server and find the dlls.
    Let me know, if you need any more information.

    ReplyDelete
  3. Thank you.
    Currently we use wss 3.0.
    Can I create Client site and server site objects with wss 3.0? Or can I use Vs2010 to develop web sites within share point?

    ReplyDelete
  4. Hi my dear friend,
    If you are using wss 3.0, then this is not the right way to do it. Client Object Model is only introduced in the SharePoint 2010 and SharePoint foundation [Wss 4.0]. So, for you case, you need to use SharePoint server side object model, which is completely C#/VB managed code. So, by using VS 2010 it is not recommended to code for SharePoint 2007/Wss 3.0 as it is developed mainly for SharePoint 2010 development.

    If you want to write code for create site or do some logic then go for VS 2005/VS 2008 and server side SharePoint object model.
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx

    Let me know, if you need anything else.

    -Praveen.

    ReplyDelete
  5. Hi Friends,
    Can i use Client side object model with Visual Studio 2008?

    From where i can download two dll Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll ?

    ReplyDelete
  6. Hi Shailesh,
    I am sure, you understand that this is only applicable to the SharePoint 2010 server. So, all the dlls will be on the SharePoint 2010 server. As I mentioned they will be available at the location "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI"

    This path is valid only on the SharePoint 2010 server. [I am assuming your windows program files folder is c:\program files].

    Let me know, if you need any help or if you have any questions....

    thanks
    -Praveen.

    ReplyDelete
  7. hi praveen
    im new to all this...i use ironpython to access sharepoint 2007 before..now i want to do the same to access 2010..can you give me some hints?

    ReplyDelete
  8. Hi there,
    I don't know SharePoint 2010 has IronPython support. The client object model is not the server side API. They are made for Client API's. The only possible API versions are
    1. ECMAScript API, Using Javascript [http://praveenbattula.blogspot.com/2010/02/sharepoint-2010-client-object-model.html]
    2. Silverlight API Using C# [http://praveenbattula.blogspot.com/2010/03/sharepoint-2010-silverlight-client.html]
    3. Managed Client Object Model [Using C#] - [http://praveenbattula.blogspot.com/2010/03/sharepoint-2010-client-object-model.html]

    So, I think that answered your question....?

    ReplyDelete
  9. Hi Praveen,
    Thanks for reply...
    I want to know that how we can add attachment to a list item using Client Object Model. I have tried almost all way but no success. Have you any idea about that?

    I get same error that explained in this link:
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/b0113d7c-5ec7-4a83-b113-7d76dd65157a

    Thanks!!!
    Regards,
    shailesh

    ReplyDelete
  10. Excellent. The collection of samples that you've created were very helpful.

    thanks a lot Praveen

    ReplyDelete
  11. Hi,

    Microsoft released an installer, to install the sharepoint client dll's on a non sharepoint pc.

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b4579045-b183-4ed4-bf61-dc2f0deabe47

    If people are having trouble with access on the sharepoint server withholding them to get the dll's this is an other way.

    ReplyDelete
  12. Hi, Praveen.
    I have an Error when using Managed Client Object Model on Sharepoint Application Page.
    The Error is not explicit. What is the problem exactly ?

    Thanks

    ReplyDelete
  13. Hi Praveen, nice post.. but i am having small doubt, i have copied those two dll's in client machine & we are displaying the title based n your code, my doubt is if i give one more site collection name that was developed in different SharePoint server if i give that site-collection name but i couldn't able get the title i am getting error.. "Client request Exception was handled" like that. So do i need to copy those two dll's from that different SharePoint server again & do i need to reference these two dll's. please clarify ..

    ReplyDelete
  14. Hi friends,

    Your Local machine does not contain SharePoint dlls. Please refer the below link and download the files. Run the installer that matches your system architecture, either x86 or x64. After installation refer this path in your machine C:\Program Files\Common Files\Microsoft Shared\SharePoint Client. Add Microsoft.SharePoint.Client.dll,Microsoft.SharePoint.Client.Runtime.dlls as reference to your project.

    Thanks,
    Phani

    ReplyDelete