Month: November 2011

Upload large documents to SharePoint site using WebClient class

One of the best way to programmatically upload large documents to SharePoint site is by leveraging the UploadAsync method of WebClient class. The WebClient class internally uses the WebDav protocol. This approach can be used for uploading documents to SharePoint Online (Office 365) as well. The only limitation with this approach is that we’ll not able to set the meta-data of the documents using WebClient class. Here is the code-snippet for the same:-

 WebClient oWebClient = new WebClient();

oWebClient.UseDefaultCredentials = true;
byte[] bFile = System.IO.File.ReadAllBytes(@"C:SundarWEB315.wmv");
string ulr = @"http://lt010593/Shared Documents/WEB315.wmv";
System.Uri oUri = new System.Uri(ulr);

oWebClient.UploadDataAsync(oUri, "PUT", bFile);
oWebClient.UploadDataCompleted += new UploadDataCompletedEventHandler(oWebClient_UploadDataCompleted);

 Subscribe to my blog

SPWebService.ContentService returning System.NullReferenceException

When I tried to access SPWebService.ContentService in the following code-snippet (executed inside a console application), I was getting Null value.

 SPWebService contentService = SPWebService.ContentService;
            contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;

            SPWcfServiceSettings csomWcfSettings = new SPWcfServiceSettings();
            csomWcfSettings.MaxReceivedMessageSize = 104857600; // 100MB
            contentService.WcfServiceSettings["client.svc"] = csomWcfSettings;


The resolution is to change the target CPU type in the Build properties from X86 to X64 or AnyCPu Type. That fixed my issue.

Programmatically add attachments to custom list using SharePoint 2010 Client Object Model

One of the important capability of SharePoint 2010 Managed Client Object model it its ability to add attachments to custom list. This capability is provided by the Microsoft.SharePoint.Client.File class in the client side APIs. The whole idea is to directly invoke the SaveBinaryDirect method of the Microsoft.SharePoint.Client.File class and pass the parameters like ClientContext, attachment path, filestream object and a boolean flag to set the overwrite status.

  using (ClientContext clientContext = new ClientContext("http://yoursitecollection"))

                Web oWeb = clientContext.Web;
                List oList = oWeb.Lists.GetByTitle("NewCustomList");               
                FileStream oFileStream = new FileStream(@"C:SundarTestFile.txt",FileMode.Open);
                string attachmentpath = "/Lists/NewCustomList/Attachments/2/TestFile.txt";
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, attachmentpath, oFileStream, true);             


The important point to consider here is how to frame the attachment path url for adding attachment. The attachment url needs to be in the following format :-


NameoftheList – is the name of the custom list where you want you add attachments

ItemNumber – is the id of the custom list item

AttachmentFile.extension – is the file to be attached

 Subscribe to my blog

Programmatically upload documents using SharePoint 2010 Client Object Model

The following code snippet helps us to programmatically upload documents to document library, using Client Object Model.

 using (ClientContext clientContext = new ClientContext("http://yoursitecollection"))

                Web oWeb = clientContext.Web;
                List oList = oWeb.Lists.GetByTitle("Shared Documents");               

                FileCreationInformation oFileCreationInformation = new FileCreationInformation();                  
                byte[] filecontent=System.IO.File.ReadAllBytes(@"C:SundarTestFile.txt");
                oFileCreationInformation.Content = filecontent;
                oFileCreationInformation.Url = @"http://yoursitecollection/Shared Documents/TestFile.txt";              



 Subscribe to my blog

Enabling Code Coverage for SharePoint 2010 Automated Unit Tests

I was wondering how to enable code-coverage for SharePoint 2010 automated unit test projects in Visual Studio 2010. Here are the steps :-

1. Double click on Local.testsettings


2. Go to Data and Diagnostics


3. Choose the list of assemblies to be instrumented


Locate the assembly file (.exe, .dll, or .ocx) that you want to include in code coverage and then click Open. The file is added to the list

4. If the source assemblies (to be instrumented) have a strong name, we need to re-sign those assemblies. Create a new key file that can be used for resigning assemblies. More details about the re-signing of assemblies can be found in this article .

Run the required unit tests for SharePoint code, now you will get an option to view code-coverage.

 Subscribe to my blog