3

私にはもうアイデアはありません。私はSharepoint Listに新しい要素を作成し、ファイルを添付ファイルとして追加したいと考えています。 ListItemで新しい要素を作成できます。しかし、ファイルはアップロードされません。C# - Microsoft.SharePoint.Client.ListItemに添付ファイルとしてファイルを追加する(SharePoint 2010)

SaveBinaryDirect()機能とAttachmentCreationInformation()クラスを試してみます。私がしようとした場合 は私が

例外がスローされた、この例外が出ます:「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; 
    } 

EDIT!:

私は大きなミスを犯してきました。 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.codeprojectcom/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> 

は、属性「住所」です。これにはWeb参照のアドレスが含まれています。しかし、正しい住所は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

動作しませんでした。私はline 'clientContext.ExecuteQuery()'の行の 'listItem.Update()'の後に例外が発生します。 例外がスローされました:Microsoft.SharePoint.Client.Runtime.dllの 'Microsoft.SharePoint.Client.ServerException' 追加情報:ファイルまたはフォルダ名には、許可されていない文字が含まれています。別の名前を使用してください。 (Google Translator ...) – Duny

+0

パスに特別なスラッシュが含まれていないことを確認してください。 'https:// sitecollurl/test/lists/libraryname/foldername'のようになります –

+0

これは既に私のコードで与えられています。しかし、それは本当の問題ではありません。 添付ファイルをListItemに追加します。 – Duny

関連する問題