开发者

How to upload a file to a SharePoint library via one of the standard webservices

开发者 https://www.devze.com 2023-01-14 19:00 出处:网络
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 (integ

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 WebSe开发者_Go百科rvices of SharePoint 2010?


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();
    }
}


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号