C#-将文件作为附件添加到Microsoft.SharePoint.Client.ListItem(SharePoint 2010)

C#-将文件作为附件添加到Microsoft.SharePoint.Client.ListItem(SharePoint 2010),c#,sharepoint,sharepoint-2010,sharepoint-clientobject,splistitem,C#,Sharepoint,Sharepoint 2010,Sharepoint Clientobject,Splistitem,我没有更多的想法。我想在Sharepoint列表中创建一个新元素,并添加一个文件作为附件。我可以使用ListItem创建新元素。但该文件将不会上载 我尝试使用SaveBinaryDirect()函数和AttachmentCreationInformation()类。 如果我尝试,我会得到这个例外 引发异常:System.dll中的“System.Net.WebException”(400) 我还连接了SharePoint服务器并查看了Windows日志,但什么也没找到 在我的代码中,我使用lLi

我没有更多的想法。我想在Sharepoint列表中创建一个新元素,并添加一个文件作为附件。我可以使用
ListItem
创建新元素。但该文件将不会上载

我尝试使用
SaveBinaryDirect()
函数和
AttachmentCreationInformation()
类。 如果我尝试,我会得到这个例外

引发异常:System.dll中的“System.Net.WebException”(400)

我还连接了SharePoint服务器并查看了Windows日志,但什么也没找到

在我的代码中,我使用
lLibrary.AddItem(itemCreateInfo)
添加了一个新的
ListItem
。使用
列表项
我在SharePoint中创建了一个新元素。此元素位于SharePoint文件夹中。这一切都很好

我试了很多,但都没用。我需要帮助,求你了

下面是我的完整代码:

public bool UploadToSharepoint(string sURL, string sLibrary, string sFolderName, string sTitel, string sDescription, string sFilePath)
    {
        using (ClientContext clientContext = new ClientContext(sURL))
        {
            if (!SetCredentialsInClientContext(clientContext))
            {
                return false;
            }

            List lLibrary = clientContext.Web.Lists.GetByTitle(sLibrary);

            clientContext.Load(clientContext.Web.Lists);
            clientContext.Load(lLibrary, l => l.RootFolder.ServerRelativeUrl);

            clientContext.ExecuteQuery();

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            if (!string.IsNullOrEmpty(sFolderName))
            {
                itemCreateInfo.FolderUrl = lLibrary.RootFolder.ServerRelativeUrl + "/" + sFolderName;
            }

            ListItem newItem = lLibrary.AddItem(itemCreateInfo);

            #region Work only with Document list in SharePoint
            //using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
            //{
            //    clientContext.Load(lLibrary.RootFolder);
            //    clientContext.ExecuteQuery();
            //    string fileUrl = string.Format("{0}/{1}", lLibrary.RootFolder.ServerRelativeUrl, fi.Name);
            //    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
            //}  // Ende using 'FileStream'
            #endregion

            using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
            {
                #region WORK!
                newItem["Title"] = sTitel;
                newItem["Description"] = sDescription;
                newItem.Update();
                clientContext.ExecuteQuery();
                #endregion

                #region SaveBinaryDirect Example NOT WORK
                //using (FileStream strm = new FileInfo(sFilePath).Open(FileMode.Open))
                //{
                //    Uri url = new Uri(sURL);
                //    //Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, url.AbsolutePath + "/Attachments/" + newItem.Id + "/" + fi.Name, strm, true);
                //    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, lLibrary.RootFolder.ServerRelativeUrl + "/Attachments/" + newItem.Id + "/" + fi.Name, strm, true);
                //}

                ////Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "", fs, true);
                //string serverRelativeUrl = lLibrary.RootFolder.ServerRelativeUrl;
                //Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeUrl, fs, true);
                #endregion

                #region AttachmentCreationInformation Example NOT WORK
                AttachmentCreationInformation attInfo = new AttachmentCreationInformation();
                attInfo.FileName = fs.Name;
                attInfo.ContentStream = fs;
                newItem.AttachmentFiles.Add(attInfo);
                newItem.Update();
                clientContext.ExecuteQuery();
                #endregion
            }
        }
        return true;
    }
编辑!:

我犯了一个大错误。Sharepoint的发布日期是2010年。 因此,
AttachmentFiles.Add()
函数不起作用

我发现我必须添加服务引用并更改代码。 有关更多信息,请访问

但现在我得到了例外500。这就是我尝试连接到测试SharePoint的原因。在那里,我可以通过消息
读取日志信息,该列表不存在。
所选页面包含不存在的列表。该列表可能已被其他用户删除。

我不知道我必须为函数
AddAttachment()
指定什么
listName
属性才能找到列表

我的新代码:

public bool UploadToSharepoint(string sURL, string sLibrary, string sFolderName, string sTitel, string sDescription, string sFilePath)
    {
        using (ClientContext clientContext = new ClientContext(sURL))
        {
            if (!SetzeCredentialsInClientContext(clientContext))
            {
                return false;
            }

            List lLibrary = clientContext.Web.Lists.GetByTitle(sLibrary);

            clientContext.Load(lLibrary);
            clientContext.Load(lLibrary.RootFolder);
            clientContext.ExecuteQuery();

            ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
            if (!string.IsNullOrEmpty(sFolderName))
            {
                listItemCreationInformation.FolderUrl = lLibrary.RootFolder.ServerRelativeUrl + "/" + sFolderName;
            }

            var newItem = lLibrary.AddItem(listItemCreationInformation);
            newItem["Title"] = sTitel;
            newItem.Update();
            clientContext.ExecuteQuery();

            clientContext.Load(newItem);
            clientContext.ExecuteQuery();

            TestSP.ListsSoapClient lsc = new TestSP.ListsSoapClient();

            if (_cbAutorisierung.Checked)
            {
                lsc.ClientCredentials.Windows.ClientCredential = new NetworkCredential(tbName.Text, tbPasswort.Text, tbDomain.Text);
            }
            else
            {
                lsc.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            }
            lsc.AddAttachment(sLibrary, newItem["ID"].ToString(), Path.GetFileName(sFilePath), System.IO.File.ReadAllBytes(sFilePath));

        }
        return true;
    }

请尝试下面的代码。已修改文件夹url的形成方式。还修改了一些与附件上传相关的代码

sUrl
中,请传递您网站集的完整url

var list = web.Lists.GetByTitle(sLibrary);
clientContext.Load(list);
clientContext.ExecuteQuery();

ListItemCreationInformation listItemCreationInformation = null;
if (!string.IsNullOrEmpty(sFolderName))
{
    listItemCreationInformation = new ListItemCreationInformation();
    listItemCreationInformation.FolderUrl = string.Format("{0}Lists/{1}/{2}", sURL, sLibrary, sFolderName);
}

var listItem = list.AddItem(listItemCreationInformation);
newItem["Title"] = sTitel;
newItem["Description"] = sDescription;
listItem.Update();
clientContext.ExecuteQuery();                

using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
{
    var attInfo = new AttachmentCreationInformation();
    attInfo.FileName = fs.Name;
    attInfo.ContentStream = fs;                    

    var att = listItem.AttachmentFiles.Add(attInfo);
    clientContext.Load(att);
    clientContext.ExecuteQuery();
}
我终于开始工作了

问题在于,在配置文件
App.config
中,端点指向站点的显式引用

“配置”文件的部分:



标记“endpoint”处是属性“address”。这包含web引用的地址。但是正确的地址必须是
address=”https://TestSharePoint.com/TestSite/TestUnderSite/_vti_bin/lists.asmx“

不起作用。我在“listItem.Update()”行之后的“clientContext.ExecuteQuery()”行上遇到异常。引发异常:Microsoft.SharePoint.Client.Runtime.dll中的“Microsoft.SharePoint.Client.ServerException”其他信息:文件或文件夹名称包含不允许的字符。请使用其他名称。(谷歌翻译…)确保路径不包含额外的前斜杠。应该是“我的代码中已经给出了”。但这不是真正的问题。我想向ListItem添加附件。请使用已编辑的代码进行检查。已删除文件夹url路径中的正斜杠。我已经在我的环境中测试了上面的代码,并且运行良好。它创建了一个带有附件的列表项。我试过了,但遇到了这个问题。我将代码从“listItemCreationInformation.FolderUrl=string.Format(“{0}Lists/{1}/{2}”,sURL,sLibrary,sFolderName);”更改而来至“listItemCreationInformation.FolderUrl=lLibrary.RootFolder.ServerRelativeUrl+”/“+sFolderName;”因为“sURL”类似于“”。代码的工作方式是创建元素。和以前一样。但现在我收到一条新的异常消息:“Microsoft.SharePoint.Client.ServerException:根级别的数据无效。”。第2行,位置1'
<system.serviceModel>
<bindings>
  <basicHttpsBinding>
    <binding name="ListsSoap">
      <security>
        <transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" />
      </security>
    </binding>
  </basicHttpsBinding>
</bindings>
<client>
  <endpoint address="https://TestSharePoint.com/_vti_bin/lists.asmx"
    binding="basicHttpsBinding" bindingConfiguration="ListsSoap"
    contract="SPRivarkom.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>