Monday, August 27, 2012

Uploading documents into SharePoint 2010 using OData

I experienced some pain when asked to develop a utility to upload documents into SharePoint 2010 using OData. Therefore, I naturally decided on this post in order to save others the pain I went through. This post explains how to do it using a simple client-side C# command-line application:

Step 1:

Create a C# command-line application in Visual Studio 2010 on the same box as your SharePoint 2010 server. Add a proxy to the SharePoint 2010 OData service on your server. This is done by right-clicking on your project “References” node and choosing “Add Service Reference …”.

image

The service that needs to be consumed is listdata.svc. Enter a URL similar to http://myserver/_vti_bin/listdata.svc then click on the “Go” button. Once the service is found, give the service NameSpace the name ListdataServiceReference.

Step 2:

I placed a text file named bogus.txt in the root of my c: drive with some arbitrary content. I also created a document library named BogusDocumentLibrary in the root site of my SharePoint server. Here is the upload() method code for uploading file c:\bogus.txt into the document library named BogusDocumentLibrary.

private static void upload() {
    string sharePointSvc = "
http://myserver/_vti_bin/listdata.svc";

    using (FileStream file = File.Open(@"c:\bogus.txt", FileMode.Open)) {
        ListdataServiceReference.HomeDataContext ctx
            = new ListdataServiceReference.HomeDataContext(new Uri(sharePointSvc));

       // ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;

        string username = "alice";
        string password = "wonderland";
        string domain = "myserver";

        ctx.Credentials = new System.Net.NetworkCredential(username, password, domain);

        string path = "/BogusDocumentLibrary/Bogus.txt";
        string contentType = "plain/text";
        ListdataServiceReference.BogusDocumentLibraryItem documentItem = new ListdataServiceReference.BogusDocumentLibraryItem()
        {
            ContentType = contentType,
            Name = "Bogus",
            Path = path,
            Title = "Bogus"
        };

        ctx.AddToBogusDocumentLibrary(documentItem);

        ctx.SetSaveStream(documentItem, file, false, contentType, path);

        ctx.SaveChanges();
    }
}

Needless to say, you must resolve the missing System.IO namespace.

Context
The name of the proxy class representing your context is named depending on your site’s name. In my case, my site is named home so the proxy context class is HomeDataContext.

Credentials
If you are accessing the service on the same box or domain as the server then it suffices that you pass on the default credentials with “System.Net.CredentialCache.DefaultCredentials”. Otherwise, if you are on a different domain then you should use the technique described in the code above where the username, password, and domain with access rights to the SharePoint site are passed on to the server.

Document Properties
The item proxy class name that represents the document library depends on the name given to it. In my case, I named the document library BogusDocumentLibrary so the item proxy class name is BogusDocumentLibraryItem. Once an instance of this class in instantiated then the various properties such as ContentType, Name, Path, and Title must be set.

Upload document
Finally, these three lines are responsible for uploading the document:

ctx.AddToBogusDocumentLibrary(documentItem);
ctx.SetSaveStream(documentItem, file, false, contentType, path);
ctx.SaveChanges();

Finally, you can call the upload() method from within your main method and it should all work as long as the appropriate server, text file, and document library exist.

2 comments:

  1. I think this is geat but I need to know how I can upload a folder in this way.

    regards.

    ReplyDelete
  2. How to update the file and field a single request?

    ReplyDelete