示例#1
0
        /// <summary>
        /// send File to Sharepoint
        /// </summary>
        /// <param name="ms">the memory stream holding the data</param>
        /// <param name="formToSpLibrary"> Form to sp library to get config data </param>
        /// <param name="data"> The data that holds the dataId and the formId</param>
        /// <param name="metadatas">collection<Datamaping> tha holds the metadata</param>
        /// <param name="filePath">the filePath</param>
        /// <param name="fileName_">the fileName</param>
        public void SendToSpLibrary(MemoryStream ms, List formsLibrary, List <DataMapping> metadatas, FormData data, string filePath, string fileName_)
        {
            FileCreationInformation fcInfo = new FileCreationInformation();

            fcInfo.Url       = Path.Combine(filePath, fileName_);
            fcInfo.Overwrite = true;
            fcInfo.Content   = ms.ToArray();

            Log.Debug("-----Configuring file destination in Sharepoint");
            Log.Debug($"-----File url : {fcInfo.Url}");

            lock (locky)
            {
                try
                {
                    Microsoft.SharePoint.Client.File uploadedFile = formsLibrary.RootFolder.Files.Add(fcInfo);

                    if (metadatas.Count != 0)
                    {
                        FillFileMetaDatas(metadatas, uploadedFile, data).Wait();
                    }

                    uploadedFile.ListItemAllFields.Update();
                    uploadedFile.Update();

                    Context.ExecuteQuery();
                    Log.Debug($"File : {fcInfo.Url}  sent to splibrary successfully");
                }
                catch (Exception Ex)
                {
                    TOOLS.LogErrorwithoutExitProgram(Ex.Message + $"\n" + " filepath : error while uploading file :" + fcInfo.Url);
                    TOOLS.LogErrorwithoutExitProgram("This error appears when an illegal character is detected in file path: /, \\");
                }
            }
        }
示例#2
0
        /// <summary>
        /// send file to sp library
        /// </summary>
        /// <param name="ms">the memory stream holding the export</param>
        /// <param name="spLibrary">Guid of spLibrary</param>
        /// <param name="filePath">the file path</param>
        /// <param name="fileName_">the fileName</param>
        private void SendPeriodicExportToSpLibrary(MemoryStream ms, List spLibrary, string filePath, string fileName_)
        {
            FileCreationInformation fcInfo = new FileCreationInformation();

            fcInfo.Url       = Path.Combine(filePath, fileName_);
            fcInfo.Overwrite = true;
            fcInfo.Content   = ms.ToArray();

            Log.Debug("-----Configuring file destination in Sharepoint");
            Log.Debug($"-----File url : {fcInfo.Url}");

            lock (locky)
            {
                try
                {
                    Microsoft.SharePoint.Client.File uploadedFile = spLibrary.RootFolder.Files.Add(fcInfo);

                    uploadedFile.ListItemAllFields.Update();
                    uploadedFile.Update();

                    Context.ExecuteQuery();
                    Log.Debug($"File : {fcInfo.Url}  sent to splibrary successfully");
                }
                catch (Exception Ex)
                {
                    TOOLS.LogErrorwithoutExitProgram(Ex.Message + $"\n" + " filepath : error while uploading file :" + fcInfo.Url + "\n Stack Trace : " + Ex.StackTrace);
                }
            }
        }
示例#3
0
        public Boolean setTypeDescPropsOfFile()
        {
            Boolean tmpRetVal = true;

            Microsoft.SharePoint.Client.File tmpFile = null;
            ClientContext ctx         = null;
            String        tmpRelPath  = String.Empty;
            String        tmpFileType = String.Empty;
            String        tmpFileDesc = string.Empty;

            try
            {
                if (SPFileType != String.Empty && SPFileDescription != String.Empty)
                {
                    tmpFileType = SPFileType;
                    tmpFileDesc = SPFileDescription;

                    ctx = new ClientContext(SPSiteURL);
                    using (ctx)
                    {
                        tmpRelPath = SPRelPath + "/" + SPFileName;
                        tmpRelPath = tmpRelPath.Replace(" ", "%20");
                        tmpFile    = ctx.Web.GetFileByServerRelativeUrl(tmpRelPath);


                        //var item = tmpFile.ListItemAllFields;
                        //var fields = item.ParentList.Fields;

                        //ctx.Load(tmpFile, f => f.ListItemAllFields);
                        ctx.Load(tmpFile);
                        //ctx.Load(fields, include => include.Include(f => f.Id, f => f.InternalName, f => f.Title));
                        ctx.ExecuteQuery();

                        if (tmpFileDesc != String.Empty)
                        {
                            tmpFile.ListItemAllFields[DescFldName] = tmpFileDesc;
                        }
                        if (tmpFileType != String.Empty)
                        {
                            tmpFile.ListItemAllFields[typFldName] = SPFileType;
                        }

                        //item.Update();
                        tmpFile.Update();
                        ctx.ExecuteQuery();
                        //Debug.WriteLine(item["DocType"]);
                    }
                }
            }
            catch (Exception ex)
            {
                tmpRetVal     = false;
                RetErrMessage = ex.Message;
            }
            return(tmpRetVal);
        }
        public static void UploadFile(ClientContext context)
        {
            List DestList = context.Web.Lists.GetByTitle("DemoLib");
            FileCreationInformation FileCreationInformation = new FileCreationInformation();

            FileCreationInformation.Content   = System.IO.File.ReadAllBytes(@"D:\harsha853\SharePointAssessment.xlsx");
            FileCreationInformation.Overwrite = true;
            FileCreationInformation.Url       = "DemoLib/SharePointAssessment.xlsx";
            Microsoft.SharePoint.Client.File uploadfile = DestList.RootFolder.Files.Add(FileCreationInformation);
            uploadfile.Update();
            context.ExecuteQuery();
        }
示例#5
0
        protected override async Task ActivateAsync()
        {
            var contactListName  = "Contact List";
            var taskListName     = "Onboarding Tasks";
            var documentListName = "Documents";

            var contacts = new List <string> {
                "Thomas,Mod,[email protected],112, Admin",
                "David,De Gea,[email protected],113, Goal Keeper",
                "Luke,Shaw,[email protected],114, Left Back"
            };

            var tasks = new List <string> {
                $"Greeting to everyone;[email protected]; {DateTime.Now.ToString()};{DateTime.Now.AddHours(4).ToString()}"
            };

            var spUrl  = AppInstance.Properties.ContextParams.EnsureContextParamStringValue(Omnia.Fx.SharePoint.Constants.Parameters.SPUrl);
            var ctx    = SharePointClientContextProvider.CreateClientContext(spUrl);
            var spSite = ctx.Site;

            var contactList     = ctx.Web.Lists.GetByTitle(contactListName);
            var taskList        = ctx.Web.Lists.GetByTitle(taskListName);
            var documentList    = ctx.Web.Lists.GetByTitle(documentListName);
            var documentFolders = documentList.RootFolder.Folders;
            var web             = ctx.Web;

            ctx.Load(contactList);
            ctx.Load(taskList);
            ctx.Load(documentList);
            ctx.Load(documentFolders);
            ctx.Load(web);

            await ctx.ExecuteQueryAsync();

            //Add data to contact list
            foreach (var contact in contacts)
            {
                var itemCreation = new ListItemCreationInformation();
                var oContact1    = contactList.AddItem(itemCreation);

                oContact1["First_x0020_Name"]   = contact.Split(',')[0];
                oContact1["Last_x0020_Name"]    = contact.Split(',')[1];
                oContact1["Email"]              = contact.Split(',')[2];
                oContact1["Phone_x0020_number"] = contact.Split(',')[3];
                oContact1["Job_x0020_Title"]    = contact.Split(',')[3];
                oContact1.Update();
                await ctx.ExecuteQueryAsync();
            }

            //Add data to task list
            foreach (var task in tasks)
            {
                Microsoft.SharePoint.Client.User user = ctx.Web.EnsureUser(task.Split(';')[1]);
                ctx.Load(user);
                await ctx.ExecuteQueryAsync();

                var toBeAddUser = new FieldUserValue()
                {
                    LookupId = user.Id
                };

                var itemCreation = new ListItemCreationInformation();
                var oTask        = taskList.AddItem(itemCreation);

                oTask["Title"]     = task.Split(';')[0];
                oTask["Contact"]   = toBeAddUser;
                oTask["StartDate"] = task.Split(';')[2];
                oTask["DueDate"]   = task.Split(';')[3];
                oTask.Update();
                await ctx.ExecuteQueryAsync();
            }

            /// Documents list
            var srcUrl    = spUrl.Substring(0, spUrl.LastIndexOf("/sites"));
            var srcCtx    = SharePointClientContextProvider.CreateClientContext($"{srcUrl}/sites/onboarding-source");
            var camlQuery = CamlQuery.CreateAllItemsQuery();
            var srcList   = srcCtx.Web.Lists.GetByTitle("Documents");
            var srcItems  = srcList.GetItems(camlQuery);

            srcCtx.Load(srcList, l => l.RootFolder, l => l.RootFolder.Folders);
            srcCtx.Load(srcItems, items => items.Include(i => i.File));
            await srcCtx.ExecuteQueryAsync();

            foreach (var folder in srcList.RootFolder.Folders)
            {
                var existedFolder = documentFolders.FirstOrDefault(f => f.Name == folder.Name);
                var folderUrl     = "";
                if (existedFolder == null)
                {
                    documentList.EnableFolderCreation = true;
                    documentList.Update();
                    await ctx.ExecuteQueryAsync();

                    ListItemCreationInformation folderCreationInfo = new ListItemCreationInformation();
                    folderCreationInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
                    folderCreationInfo.LeafName             = folder.Name;

                    var newItem = documentList.AddItem(folderCreationInfo);
                    newItem["Title"] = folder.Name;
                    newItem.Update();
                    await ctx.ExecuteQueryAsync();

                    ctx.Load(newItem);
                    await ctx.ExecuteQueryAsync();

                    folderUrl = newItem.FieldValues["FileRef"].ToString();
                }
                else
                {
                    folderUrl = existedFolder.ServerRelativeUrl;
                }
                //var srcItem = srcItems.FirstOrDefault(item => item.FieldValues["FileLeafRef"])
                var folderItems = folder.Files;
                srcCtx.Load(folderItems);
                await srcCtx.ExecuteQueryAsync();

                foreach (var file in folderItems)
                {
                    var fileStream = file.OpenBinaryStream();
                    await srcCtx.ExecuteQueryAsync();

                    var fileCreationInfo = new FileCreationInformation();
                    fileCreationInfo.ContentStream = fileStream.Value;
                    fileCreationInfo.Overwrite     = true;
                    Uri result = null;
                    if (Uri.TryCreate(new Uri(ctx.Web.Url), folderUrl, out result))
                    {
                        fileCreationInfo.Url = result.ToString() + "/" + file.Name;
                    }
                    Microsoft.SharePoint.Client.File uploadFile = documentList.RootFolder.Files.Add(fileCreationInfo);
                    uploadFile.Update();

                    await ctx.ExecuteQueryAsync();

                    if (file.Title != "" && file.Title != null)
                    {
                        uploadFile.ListItemAllFields["Title"] = file.Title;
                        uploadFile.Update();
                        await ctx.ExecuteQueryAsync();
                    }
                }
            }
        }