3

나는 더 이상의 아이디어가 없습니다. 셰어 포인트 목록에 새 요소를 만들고 파일을 첨부 파일로 추가하려고합니다. ListItem으로 새 요소를 만들 수 있습니다. 그러나 파일은 업로드되지 않습니다.첨부 파일을 Microsoft.SharePoint.Client.ListItem에 첨부 파일로 추가 (SharePoint 2010)

SaveBinaryDirect() 기능과 AttachmentCreationInformation() 클래스로 시도합니다. 내가하려고하면 나는

예외가 throw이 예외를 얻을 : 'System.Net.WebException'을 System.dll을에 (400)

가 나는 또한 SharePoint 서버에 연결하고 Windows 로그 내부 모습 ,하지만 나는 아무것도 발견하지 못했습니다.

내 코드에 ListItemlLibrary.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() 기능이 작동하지 않습니다.

서비스 참조를 추가하고 코드를 변경해야한다는 것을 알았습니다. 더 많은 정보는 SharePoint 2010 - Client Object Model - Add attachment to ListItem

에서 찾을 수 있습니다.하지만 이제 Exception 500이 생겼습니다. 그래서 Test 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()에 해당 기능을 지정해야하는지 알 수 없습니다.

나의 새로운 코드 :

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; 
    } 
+0

https : //www.codeproject.com/Questions/602369/C-pluscodeplustoplusuploadplusdocumentploploplus – saj

답변

1

마지막으로 작업해야합니다!

문제는 구성 파일 App.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"입니다. 여기에는 웹 참조의 주소가 포함됩니다. 그러나 올바른 주소는 address="https://TestSharePoint.com/TestSite/TestUnderSite/_vti_bin/lists.asmx"이어야합니다.

0

아래의 코드를 시도하십시오. 폴더 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(); 
} 
+0

작동하지 않았습니다. 'listItem.Update()'줄 다음에 'clientContext.ExecuteQuery()'예외가 발생합니다. 예외가 throw되었습니다. Microsoft.SharePoint.Client.Runtime.dll의 'Microsoft.SharePoint.Client.ServerException' 추가 정보 : 파일 또는 폴더 이름에 허용되지 않는 문자가 포함되어 있습니다. 다른 이름을 사용하십시오. (Google Translator ...) – Duny

+0

경로에 추가 슬래시가 포함되어 있지 않은지 확인하십시오. 'https : // sitecollurl/test/lists/libraryname/foldername'과 같아야합니다. –

+0

이미 내 코드와 함께 제공됩니다. 하지만 그건 진짜 문제가 아닙니다. 첨부 파일을 ListItem에 추가하려고합니다. – Duny