Today I’m going to show you how to upload documents to a document library in SharePoint 2010 and also to create folders via web services in the document library.
I configured a document library where I want to create folders and store documents. I named it “My Documents Library” located at “http://server/sites/personal/My Documents Library”
Let’s Create Folders
In your .Net project you have to add a service reference to http://server/sites/personal/_vti_bin/Dws.asmx. This is the Document Workspace service.
Here is the code to create a folder in the document library:
1: // Title of Document Library2: string library = "My Documents Library";3:4: DwsSoapClient client = new DwsSoapClient();5: if (client.ClientCredentials != null)6: {7: client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;8: }9:10: try11: {12: // Create First Folder13: string createResult = client.CreateFolder(library + "/Folder One");14: Trace.WriteLine("Create Folder Result: " + createResult);15:16: // Create subfolder17: createResult = client.CreateFolder(library + "/Folder One/Folder Two");18: Trace.WriteLine("Create Folder Result: " + createResult);19: }20: finally21: {22: if (client.State == CommunicationState.Faulted)23: {24: client.Abort();25: }26:27: if (client.State != CommunicationState.Closed)28: {29: client.Close();30: }31: }
The first thing to watch out for is setting the client credentials on the client proxy. This tells the client to impersonate you to create the folders. You the client need to have the rights to create folders in the document library.
To create folders is very straight forward where you use the CreateFolder method. As you can see you specify the library title and forward slash with the new folder to create. I also show in the code to create subfolders.
Let’s Upload a Document
To upload a document we need to add another service reference to http://server/sites/personal/_vti_bin/copy.asmx. This is the Copy service.
Here is the code to upload a document to the document library:
1: CopySoapClient client = new CopySoapClient();2:3: if (client.ClientCredentials != null)4: {5: client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;6: }7:8: try9: {10: client.Open();11: string url = "http://server/sites/personal/My Documents Library/Folder One/Folder Two/";12: string fileName = "test.txt";13: string[] destinationUrl = { url + fileName };14: byte[] content = new byte[] { 1, 2, 3, 4 };15:16: // Description Information Field17: FieldInformation descInfo = new FieldInformation18: {19: DisplayName = "Description",20: Type = FieldType.Text,21: Value = "Test file for upload"22: };23:24: FieldInformation[] fileInfoArray = { descInfo };25:26: CopyResult[] arrayOfResults;27:28: uint result = client.CopyIntoItems(fileName, destinationUrl, fileInfoArray, content, out arrayOfResults);29: Trace.WriteLine("Upload Result: " + result);30:31: // Check for Errors32: foreach (CopyResult copyResult in arrayOfResults)33: {34: string msg = "====================================" +35: "SharePoint Error:" +36: "\nUrl: " + copyResult.DestinationUrl +37: "\nError Code: " + copyResult.ErrorCode +38: "\nMessage: " + copyResult.ErrorMessage +39: "====================================";40:41: Trace.WriteLine(msg);42: _logFactory.ErrorMsg(msg);43: }44: }45: finally46: {47: if (client.State == CommunicationState.Faulted)48: {49: client.Abort();50: }51:52: if (client.State != CommunicationState.Closed)53: {54: client.Close();55: }56: }
Again I set the client credentials to be able to upload documents to the document library. I have to specify the full URL and folder structure on where I want to upload the document too. I also have to append the filename onto the URL to upload the document.
I create a byte[] to simulate document content that will be saved in the document library. The web method that we use to upload a document only accepts an byte[] for content. If you use streams for reading a local file to upload, then you have to convert it to a byte[].
We use the CopyIntoItem method to upload the file. Firstly have to define the filename, the destination URL, file information array and a array for results that is returned.
In the example code I use the fileInfoArray to add document meta data. In the example I just add Description meta data.
When you upload the file the method returns an array of results that you can check if any errors occurred on the server for the document upload.
Hope this helps you and any feedback is welcome!
Cheerio!