Awai, delivery guy

1 hour for cheese, 1 hour for supreme. What the heck?

  Home  |   Contact  |   Syndication    |   Login
  5 Posts | 0 Stories | 5 Comments | 6 Trackbacks

News

Archives

Post Categories

Biztalk Weblogs

Monday, July 09, 2007 #

Uploading a file to a Windows SharePoint Services 3.0 document library from external applications can be done in 2 steps using a combination of HTTP PUT and the Lists Web service.

Firstly, for the HTTP PUT side of things, check out: http://blogs.msdn.com/rohitpuri/archive/2007/04/10/upload-download-file-to-from-wss-document-library-using-dav.aspx .  In the code to follow, I use a variation of Rohit's helper class that takes a byte[].

Secondly, use the Lists Web service UpdateListItems() method to do the update.  But how do we get the ID from the HTTP PUT?  Turns out you don't need it.  Providing the FileRef field is enough.  For example, if you uploaded the file to http://mycompany/hr/Documents/bonus.xls, that's exactly what you'd put in FileRef, as shown below.

        public static string UploadFile(string url, string listName, byte[] document, 
                               List<KeyValuePair<string, object>> fileProperties)
        {
            string s = DAVHelper.UploadFile(url, document);
            if(s != "success")
                throw new Exception("File upload to: " + url + " failed.");

            // Update the attributes
            string updateRes = UpdateFileAttributes(url, listName, fileProperties);
            return updateRes;
	}

        public static string UpdateFileAttributes(string fileUrl, string listName, 
                                List<KeyValuePair<string, object>> fileProperties)
        {
            WssLists.ListsSoapClient listService 
                = new WssLists.ListsSoapClient("ListsSoap");
            listService.ClientCredentials.Windows.AllowedImpersonationLevel 
                = System.Security.Principal.TokenImpersonationLevel.Delegation;

            // Note that when building the Method fields, the field named ID
            // is required, although in this case we can and will leave it empty.
	    // (Concatenated empty string is just for visuals ;)
            StringBuilder sb = new StringBuilder();
            sb.Append("<Method ID='1' Cmd='Update'>");
            sb.Append("  <Field Name='ID'/>" + "" + "</Field>");
            sb.Append("  <Field Name='FileRef'>" + fileUrl + "</Field>");

	    // This is what the loop is basically doing:
            // sb.Append("  <Field Name='CustomColumn1'>" + "Some value" + "</Field>");
            for (int i = 0; i < fileProperties.Count; i++)
            {
                sb.Append("  <Field Name='" + fileProperties[i].Key + "'>" + 
                           Convert.ToString(fileProperties[i].Value) + "</Field>");
            }

            sb.Append("</Method>");

            XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

            elBatch.SetAttribute("OnError", "Continue");
            elBatch.SetAttribute("PreCalc", "TRUE"); 
            elBatch.SetAttribute("ListVersion", "0"); 

            elBatch.InnerXml = sb.ToString();

	    // Do the update
            XmlNode ndReturn = listService.UpdateListItems(listName, elBatch);

            return ndReturn.OuterXml;
        }