2017-06-13 3 views
0

Ich versuche, eine Datei an eine Liste auf unserem Sharepoint anzuhängen.C# SharePoint-Clientliste Anlage

Ich kann das Listenelement programmgesteuert erstellen, aber ich kann den Anhang nicht anhängen. Entweder gibt es mir einen 401 nicht erlaubten Fehler (was nicht sein kann, weil ich Vollzugriff auf die Liste habe) oder die executeQuery bleibt für immer bis zum Timeout hängen.

Hier ist mein aktueller Code: (WPF-Anwendung)

 ClientContext clientContext = new ClientContext("http://<SITE-URL>/sites/Team-Place/<TeamPlace-ID>/"); 
     clientContext.RequestTimeout = int.MaxValue; 

     FileStream sr = new FileStream("Test.pdf", FileMode.Open); 
     byte[] contents = new byte[sr.Length]; 
     sr.Read(contents, 0, (int)sr.Length); 

     SP.List List = clientContext.Web.Lists.GetByTitle("<List Title>"); 

     if (List != null) 
     { 
      CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery(); 
      SP.ListItemCollection Collection = List.GetItems(camlQuery); 
      clientContext.Load(List); 
      clientContext.Load(List.Fields); 
      clientContext.Load(Collection); 
      clientContext.ExecuteQuery(); 

      foreach (var x in List.Fields) 
       Debug.AppendText(x.InternalName + "\n"); 

      ListItemCreationInformation creationInfo = new ListItemCreationInformation(); 
      SP.ListItem Item = List.AddItem(creationInfo); 

      Item["Title"] = "Test"; 
      Item["Modell"] = "Test"; 
      Item["Seriennummer"] = "testserial"; 
      Item["Ger_x00e4_te_x002d_Typ"] = "Laptop"; 

      Item.Update(); 
      clientContext.ExecuteQuery(); 
      clientContext.Load(Item); 
      clientContext.ExecuteQuery(); 

      var attInfo = new AttachmentCreationInformation(); 
      attInfo.FileName = "Test.pdf"; 
      attInfo.ContentStream = sr; 
      var att = Item.AttachmentFiles.Add(attInfo); 

      Item.Update(); 

      clientContext.Load(att); 
      clientContext.Load(Item); 
      clientContext.ExecuteQuery(); 

      //System.Diagnostics.Debug.WriteLine(att.ServerRelativeUrl); 

      Item.Update(); 
      clientContext.ExecuteQuery(); 
      /* 
      * Not working pice of S#@* 
      string attachmentPath = string.Format("/Lists/Inventur_MOBI/Attachments/{0}/{1}", Item.Id, "Test.pdf"); 
      SP.File.SaveBinaryDirect(clientContext, attachmentPath, sr, false); 
      */ 
     } 
     else 
      Debug.AppendText("List not found"); 

Ich habe "zensieren" ein Ding aus. Die SaveBinardDirect Methode gibt mir eine 401 nicht erlaubt und der Anhang gibt eine Zeitüberschreitung. Wir haben einen Sharepoint 2013.

Hat jemand eine Idee?

Grüße Bluefire

Antwort

0

Ihr Code haben zwei Probleme.

Zuerst mit Datei zu lesen, und dann mit AttachmentCreationInformation Objekt. Ändern Sie ihn auf:

byte[] contents = null; 
using (FileStream sr = new FileStream("Test.pdf", FileMode.Open)) 
{ 
    contents = new byte[sr.Length]; 
    sr.Read(contents, 0, (int)sr.Length); 
} 

//... 

using (MemoryStream ms = new MemoryStream(contents)) 
{ 
    var attInfo = new AttachmentCreationInformation(); 
    attInfo.FileName = "Test.pdf"; 
    attInfo.ContentStream = ms; 
    // ... 
} 

Zweitens, nachdem Sie Ihr neues ListItem Objekt erstellen retreive es erneut zum Schutz vor Speicherkonflikte. Verwendung:

ListItem newItem = List.AddItem(creationInfo); 
newItem["Title"] = "Test"; 
// ... 

newItem.Update(); 
clientContext.Load(newItem, i => i.Id); 
clientContext.ExecuteQuery(); 

var item = List.GetItemById(newItem.Id); 

using (MemoryStream ms = new MemoryStream(contents)) 
{ 
    // ... 
    var att = item.AttachmentFiles.Add(attInfo); 
    item.Update(); 
    clientContext.ExecuteQuery(); 
} 

Oh, und für 401 unbefugte müssen Sie Anmeldeinformationen Clientcontext weitergeben müssen:

clientContext.Credentials = new NetworkCredential("user", "password", "domain"); 
+0

es als Speicher-Stream Hochladen scheint den Trick getan zu haben. – BlueFire