C# - 将文件作为附件添加到Microsoft.sharepoint.client.listitem(SharePoi



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

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

异常抛出:'system.net.webexception'in System.dll(400)

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

在我的代码中,我使用lLibrary.AddItem(itemCreateInfo)添加了一个新的ListItem。使用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()功能无法正常工作。

我发现我必须添加服务参考并更改代码。可以在SharePoint 2010中找到更多信息 - 客户对象模型 - 将附件添加到ListItem

但是现在我得到了例外500。这就是为什么我尝试连接到测试SharePoint的原因。在那里,我可以阅读带有消息The list does not exist. The selected page contains a list that does not exist. The list could have been deleted by another user.

的日志信息

我不知道我必须为找到列表的函数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;
    }

终于我开始工作了!

问题是在配置文件App.Config中,端点adress Adress明确引用了该站点。

" config"文件的一部分:

<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>

在标签"端点"上是属性" adress"。这包含Web参考的地址。但是正确的地址必须是address="https://TestSharePoint.com/TestSite/TestUnderSite/_vti_bin/lists.asmx"

请尝试以下代码。已经修改了如何形成文件夹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();
}

相关内容

  • 没有找到相关文章

最新更新