Example #1
0
        public LibraryItem SaveToSharePoint(ClientContext context, string data, string libraryUrl, string libraryName)
        {
            string siteUrl           = GetSiteUrlFromLibraryUrl(libraryUrl);
            string serverRelativeUrl = GetServerRelativeUrlFromLibraryUrl(libraryUrl);
            string serverUrl         = GetServerUrlFromLibraryUrl(libraryUrl);
            string librarySegment    = GetLibraryUrlSegment(libraryUrl);

            LibraryItem libraryItem = new LibraryItem();
            string      fileName, fileExtension;
            int         fileSize;

            byte[] fileData;
            Base64Helper.GetBase64Values(data, out fileName, out fileExtension, out fileSize, out fileData);

            List   library        = context.Web.Lists.GetByTitle(libraryName);
            string uniqueFileName = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfffff"), fileName);
            string fileUrl        = string.Format("{0}/{1}", librarySegment, uniqueFileName);

            FileCreationInformation fileInfo = new FileCreationInformation();

            fileInfo.ContentStream = new MemoryStream(fileData);
            fileInfo.Url           = uniqueFileName;

            Microsoft.SharePoint.Client.File newFile = library.RootFolder.Files.Add(fileInfo);
            context.Load(library);
            context.ExecuteQuery();
            ListItem fileListItem = newFile.ListItemAllFields;

            context.Load(fileListItem);
            context.ExecuteQuery();
            libraryItem.Id   = fileListItem.Id;
            libraryItem.Link = new Uri(string.Format("{0}{1}", siteUrl, fileUrl));
            return(libraryItem);
        }
Example #2
0
        private void AddAttachments(ListItem item, XmlNode newNode)
        {
            AttachmentCreationInformation attInfo = new AttachmentCreationInformation();
            //attachment data is in Attachments/Attachment
            XmlNodeList attachments = newNode.SelectNodes("Attachments/Attachment");

            foreach (XmlNode attachment in attachments)
            {
                //get file attributes and file bytes
                if (string.IsNullOrEmpty(attachment.InnerText))
                {
                    return;
                }

                string attachmentName, attachmentExtension;
                int    attSize;
                byte[] fileData;
                Base64Helper.GetBase64Values(attachment.InnerText, out attachmentName, out attachmentExtension, out attSize, out fileData);

                attInfo.FileName      = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddHHmmssfffff"), attachmentName);
                attInfo.ContentStream = new MemoryStream(fileData);
                item.AttachmentFiles.Add(attInfo);
            }
        }