Awai, delivery guy

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

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

News

Archives

Post Categories

Biztalk Weblogs

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;
        }
posted on Monday, July 09, 2007 7:09 PM

Feedback

# re: Uploading files to WSS 3.0 document libraries using HTTP PUT and Lists.asmx 10/5/2007 8:12 AM Gaurav Rehan
If a person have a usecase where in he has to do following:

1) checkout via Lists.asmx
2) upload via HTTP PUT
3) checkin via Lists.asmx
4) get latest version number using Versions.asmx

This is bad...I mean 4 sync web calls one after other to do this. it should be just one web call that should do this. And I feel it requires to write a custom web service that will do these 4 steps in just one call.
Are you aware of any other way of achieving this with the help of existing web services available with WSS3.0?

Thanks,
Gaurav

# re: Uploading files to WSS 3.0 document libraries using HTTP PUT and Lists.asmx 10/24/2007 10:51 PM Mike Awai
I'm not aware of a more coarse-grained way. If you're going to repeat that pattern across use cases it's even more compelling to build the custom Web service that you describe.

# re: Uploading files to WSS 3.0 document libraries using HTTP PUT and Lists.asmx 12/17/2007 10:16 AM Yuting Zhu
For some reason, I could not set a BDC column by using Lists.asmx.

Have you ever try to set a BDC column sucessfully by using Lists.asmx?

Thanks,
Yuting

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 7 and type the answer here: