Beispiel #1
0
        private static void UploadAttachmentToSharePoint(byte[] outputFile, string fileName)
        {
            spClient.ClientContext spCtx = LoginCsom();

            spClient.List myList = spCtx.Web.Lists.GetByTitle(SpListName);
            spClient.ListItemCollection allItems = myList.GetItems(spClient.CamlQuery.CreateAllItemsQuery());
            spCtx.Load(allItems);
            spCtx.ExecuteQuery();

            foreach (spClient.ListItem oneItem in allItems)
            {
                if (fileName.Contains(oneItem["Title"].ToString()))
                {
                    spClient.AttachmentCreationInformation attInfo = new spClient.AttachmentCreationInformation();
                    attInfo.FileName      = fileName;
                    attInfo.ContentStream = new MemoryStream(outputFile.ToArray());
                    oneItem.AttachmentFiles.Add(attInfo);
                    spCtx.ExecuteQuery();
                }
            }
        }
Beispiel #2
0
        static void CreateXmlItemsInSharePoint()
        {
            spClient.ClientContext spCtx = LoginCsom();

            spClient.List myList = spCtx.Web.Lists.GetByTitle(SpListName);
            spClient.ListItemCollection allItems = myList.GetItems(spClient.CamlQuery.CreateAllItemsQuery());
            spClient.FieldCollection    myFields = myList.Fields;
            spCtx.Load(myFields);
            spCtx.Load(allItems);
            spCtx.ExecuteQuery();

            Dictionary <string, string> fieldValues = new Dictionary <string, string>();

            foreach (Microsoft.SharePoint.Client.ListItem oneItem in allItems)
            {
                MemoryStream myStream = new MemoryStream();

                XmlWriterSettings mySettings = new XmlWriterSettings
                {
                    Indent             = true,
                    IndentChars        = ("    "),
                    CloseOutput        = true,
                    OmitXmlDeclaration = true
                };

                using (XmlWriter myWriter = XmlWriter.Create(myStream, mySettings))
                {
                    myWriter.WriteStartDocument(true);
                    myWriter.WriteStartElement("Item");

                    foreach (Microsoft.SharePoint.Client.Field oneField in myFields)
                    {
                        if (oneField.Hidden == false)
                        {
                            try
                            {
                                fieldValues.Add(oneField.Title, oneItem[oneField.Title].ToString());
                            }
                            catch
                            {
                                // In case there is more than one field with the same name
                            }
                        }
                    }

                    foreach (string oneKey in fieldValues.Keys)
                    {
                        myWriter.WriteStartElement(oneKey.Replace(" ", "_"));
                        myWriter.WriteString(fieldValues[oneKey]);
                        myWriter.WriteEndElement();
                    }

                    fieldValues.Clear();
                    myWriter.WriteEndElement();
                    myWriter.WriteEndDocument();
                    myWriter.Flush();

                    try
                    {
                        spClient.AttachmentCreationInformation attInfo = new spClient.AttachmentCreationInformation();
                        attInfo.FileName      = oneItem["Title"] + ".xml";
                        attInfo.ContentStream = new MemoryStream(myStream.ToArray());
                        oneItem.AttachmentFiles.Add(attInfo);
                        spCtx.ExecuteQuery();
                    }
                    catch
                    {
                        // In case the attachment already exists
                    }
                }
            }
        }