2

I have to upload some XML files to a SharePoint library. I have to do it from a computer that is not one of the SharePoint servers (so object model will not work). Also the library has a custom (integer) column and I have to set it's value for the uploaded file.

How do I upload the file and the the value by using the standard WebServices of SharePoint 2010?

3 Answers 3

6

I now solved it by using the Client Object Model (as Doug suggested). Uploading a file using the COM is pretty simple:

public void UploadXmlFile(string xmlContent, int orderNumber)
{
    string filename = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + "_" + orderNumber + ".xml";
    ClientContext context = new ClientContext(absoluteHostUrl + relativeWebUrl);
    using (MemoryStream stream = new MemoryStream())
    {
        // use a MemoryStream for the file contents
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(xmlContent);
        writer.Flush();
        stream.Position = 0;
        // ... and upload it.
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(
            context,
            "/" + relativeWebUrl + "Lists/" + libraryName + "/" + filename,
            stream,
            false);
    }
    // ...

... but after uploading the file is checked-out, and I still have to set my integer column:

    // ...
    // get the created entry
    Web web = context.Web;
    List list = web.Lists.GetByTitle(libraryName);
    ListItemCollection itemCol = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
    context.Load(itemCol,
        items => items
            .Include(i => i[COLUMN_IMPORTORDERNUMBER])
            .Where(i => (string)i[COLUMN_FILELEAFREF] == filename)
            .Take(1)
            );
    context.ExecuteQuery();
    // ... found it? ...
    if (itemCol != null && itemCol.Count > 0)
    {
        ListItem item = itemCol[0];
        // ... set the ImportOrderNumber
        item[COLUMN_IMPORTORDERNUMBER] = orderNumber;
        item.Update();
        // ... and check in
        item.File.CheckIn("Checked in by WebService", CheckinType.MajorCheckIn);
        context.ExecuteQuery();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I haven't used it yet, but the Client Object Model may be a good choice for this.

Comments

0

Could you use something as simple as email to a list, and have the item created from that?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.