如何通过标准的web服务的一个文件上载到SharePoint库 [英] How to upload a file to a SharePoint library via one of the standard webservices

查看:139
本文介绍了如何通过标准的web服务的一个文件上载到SharePoint库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些XML文件上传到SharePoint库。我必须从不是SharePoint服务器上(这样的对象模型不能正常工作)之一的计算机做。另外,库中有一个自定义(整数)列,我必须设置它是上传文件的价值。

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.

如何通过上传文件和值标准Web服务的SharePoint 2010的?

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

推荐答案

我现在解决它通过使用客户端对象模型(道格建议)。上传使用COM文件是非常简单的:

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

这篇关于如何通过标准的web服务的一个文件上载到SharePoint库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆