//Delete Documents public string DeleteDocument(string RepoName, string DocId) { string msg; try { ClientContext ctx = new ClientContext(this.objAuth.SiteURL); ctx.RequestTimeout = 50000; ctx.Credentials = this.GetO365Credentials(this.objAuth.UserName, this.objAuth.UserPassword); Web web = ctx.Web; Microsoft.SharePoint.Client.List oList = web.Lists.GetByTitle(RepoName); ListItem DocToDelete = oList.GetItemById(DocId); DocToDelete.DeleteObject(); oList.Update(); if (ctx.HasPendingRequest) { ctx.ExecuteQueryAsync() .Wait(); } msg = "Document Removed from Repository"; } catch (Exception ex) { return("Error:" + ex.Message); } return(msg); }
public void addColumns(string listTitle) { Web web = mClientContext.Web; SP.List projList = mClientContext.Web.Lists.GetByTitle(listTitle); // Adding the Custom field to the List. Here the OOB SPFieldText has been selected as the “FieldType” SP.FieldCollection collFields = projList.Fields; collFields.AddFieldAsXml("<Field DisplayName='Estimated Start Time' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Estimated Start Date' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Estimated End Time' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Estimated End Date' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Actual Start Time' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Actual End Time' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Actual Start Date' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Actual End Date' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Milestone Number' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Milestone Comment' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Time Spent' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Resources' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Current Status' Type='Choice' />", true, AddFieldOptions.DefaultValue); collFields.AddFieldAsXml("<Field DisplayName='Current Status Reason' Type='Choice' />", true, AddFieldOptions.DefaultValue); //test field //collFields.AddFieldAsXml("<Field DisplayName='test' Type='Choice' />", true, AddFieldOptions.DefaultValue); //SP.Field oField = collFields.GetByTitle("MyNewField"); Console.WriteLine("Executing end of adding columns"); projList.Update(); mClientContext.ExecuteQuery(); }
public bool TryGetList(string listTitle) { Web web = mClientContext.Web; SP.List projList = web.Lists.GetByTitle(listTitle); try { mClientContext.ExecuteQuery(); this.mIsNewList = false; return(false); } catch (ServerException se) { try { Console.WriteLine("the hash code is: " + se.GetHashCode()); if (se.Message.Contains("does not exist at site with URL")) { //incorrect way of checking if proj list is null or not Console.WriteLine("So it is not null??"); ListCreationInformation creationInfo = new ListCreationInformation(); creationInfo.Title = listTitle; creationInfo.TemplateType = (int)ListTemplateType.GenericList; projList = web.Lists.Add(creationInfo); projList.Description = "Project Updates"; projList.Update(); mClientContext.ExecuteQuery(); mIsNewList = true; addColumns(listTitle); this.mIsNewList = true; return(true); } return(true); } catch (PropertyOrFieldNotInitializedException pfnie) { Console.WriteLine(pfnie); return(true); } } }
/// <summary> /// CreateFolder - create a folder in a document library using SharePoint Online CSOM /// </summary> /// <param name="web"></param> /// <param name="list"></param> /// <param name="folderRelativePath"></param> /// <param name="folderName"></param> /// <returns></returns> private static Microsoft.SharePoint.Client.Folder CreateFolder(Web web, Microsoft.SharePoint.Client.List list, string folderRelativePath, string folderName) { Microsoft.SharePoint.Client.Folder currentFolder = null; try { list.EnableFolderCreation = true; list.Update(); web.Context.ExecuteQuery(); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); itemCreateInfo.UnderlyingObjectType = FileSystemObjectType.Folder; itemCreateInfo.LeafName = folderName; Microsoft.SharePoint.Client.ListItem newFolder = list.AddItem(itemCreateInfo); newFolder["Title"] = folderName; newFolder.Update(); web.Context.ExecuteQuery(); Microsoft.SharePoint.Client.Folder rootFolder = list.RootFolder; web.Context.Load(rootFolder); web.Context.ExecuteQuery(); FolderCollection folders = rootFolder.Folders; web.Context.Load(folders); web.Context.ExecuteQuery(); currentFolder = list.RootFolder.Folders[list.RootFolder.Folders.Count - 1]; } catch (Exception ex) { Console.WriteLine("Error creating new folder named {0}", folderName); Console.WriteLine("Exception: {0}", ex.Message); return(null); } return(currentFolder); }
// Delete Method via Criteria private void DeletionProcessCriteria() { if (cboSiteList.SelectedItem.ToString().Contains("Archive")) { MessageBox.Show("Cannot delete items from an archive list."); } else { try { // ***************************** SP ONLINE LIST(S) *********************************** if (IsSPOnline == true) { SPOnlineContext = ClaimClientContext.GetAuthenticatedContext(cboSiteURL.SelectedItem.ToString()); SP.List selectedList = SPOnlineContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View>" + "<Query>" + "<Where>" + "<And>" + "<Geq>" + "<FieldRef Name='From' />" + "<Value Type='DateTime'>2017-01-01</Value>" + "</Geq>" + "<Leq>" + "<FieldRef Name='From' />" + "<Value Type='DateTime'>2017-12-31</Value>" + "</Leq>" + "</And>" + "</Where>" + "</Query>" + "</View>"; ListItemCollection oListitemCollection = selectedList.GetItems(camlQuery); SPOnlineContext.Load(oListitemCollection); SPOnlineContext.ExecuteQuery(); int listCount = oListitemCollection.Count; foreach (ListItem sourceListItem in oListitemCollection) { SP.List destinationList = SPOnlineContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem destinationListItem = destinationList.AddItem(itemCreateInfo); switch (cboSiteList.SelectedItem.ToString()) { case "All Grant Submitssss": for (int i = 0; i < listCount; i++) { var item = oListitemCollection[i]; // In order to prevent changes in all places inside the [for loop] item.DeleteObject(); listCount--; // Here, as I am removing 1 item, I am decrementing the count i--; // Executes the clientContext.ExecuteQuery() inside the loop to prevent the // 'The request uses too many resources' error. selectedList.Update(); SPOnlineContext.ExecuteQuery(); //MessageBox.Show("Deletion in Progress ..."); } selectedList.Update(); SPOnlineContext.ExecuteQuery(); //int endCount = oListitemCollection.Count; break; }// END Switch } MessageBox.Show("Deletion Completed!"); } else { // ***************************** SP ON-PREM LIST(S) *********************************** // SP ON PREM Selections SPOnPremContext = new ClientContext(cboSiteURL.SelectedItem.ToString()); SP.List selectedList = SPOnPremContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); string fromDate = metroDateTimeFrom.Value.ToString("yyyy-MM-dd"); string toDate = metroDateTimeTo.Value.ToString("yyyy-MM-dd"); // The following example displays the Items Created less than or earlier to 20XX // This CAMLQUERY is used for ClockInOutLog List CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View><Query><Where><Lt><FieldRef Name='Created'/>" + "<Value Type='DateTime'>" + fromDate + "</Value></Lt></Where></Query><RowLimit>200</RowLimit></View>"; ListItemCollection oListitemCollection = selectedList.GetItems(camlQuery); SPOnPremContext.Load(oListitemCollection); SPOnPremContext.ExecuteQuery(); int listCount = oListitemCollection.Count; foreach (ListItem sourceListItem in oListitemCollection) { SP.List destinationList = SPOnPremContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem destinationListItem = destinationList.AddItem(itemCreateInfo); switch (cboSiteList.SelectedItem.ToString()) { case "ClockInOutLog": for (int i = 0; i < listCount; i++) { var item = oListitemCollection[i]; // In order to prevent changes in all places inside the [for loop] item.DeleteObject(); listCount--; // Here, as I am removing 1 item, I am decrementing the count i--; // Executes the clientContext.ExecuteQuery() inside the loop to prevent the // 'The request uses too many resources' error. selectedList.Update(); SPOnPremContext.ExecuteQuery(); //MessageBox.Show("Deletion in Progress ..."); //TODO: Need Progressbar here } selectedList.Update(); SPOnPremContext.ExecuteQuery(); //int endCount = oListitemCollection.Count; break; case "TrackEmployeeTasks": for (int i = 0; i < listCount; i++) { var item = oListitemCollection[i]; // In order to prevent changes in all places inside the [for loop] item.DeleteObject(); listCount--; // Here, as I am removing 1 item, I am decrementing the count i--; // Executes the clientContext.ExecuteQuery() inside the loop to prevent the // 'The request uses too many resources' error. selectedList.Update(); SPOnPremContext.ExecuteQuery(); //MessageBox.Show("Deletion in Progress ..."); //TODO: Need Progressbar here } selectedList.Update(); SPOnPremContext.ExecuteQuery(); break; }// END Switch } MessageBox.Show("Deletion Completed!"); // Populate DGV - Refresh!! PopulateDGV(); } } catch (Exception ex) { ex.Message.ToString(); MessageBox.Show("Error Encountered " + ex.Message); } } }
// Delete Method private void DeletionProcess() { if (cboSiteList.SelectedItem.ToString().Contains("Archive")) { MessageBox.Show("Cannot delete items from an archive list."); } else { try { if (IsSPOnline == true) { SPOnlineContext = ClaimClientContext.GetAuthenticatedContext(cboSiteURL.SelectedItem.ToString()); SP.List selectedList = SPOnlineContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @""; ListItemCollection oListitemCollection = selectedList.GetItems(camlQuery); SPOnlineContext.Load(oListitemCollection); SPOnlineContext.ExecuteQuery(); int listCount = oListitemCollection.Count; foreach (ListItem sourceListItem in oListitemCollection) { SP.List destinationList = SPOnlineContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem destinationListItem = destinationList.AddItem(itemCreateInfo); switch (cboSiteList.SelectedItem.ToString()) { case "All Grant Submitssss": for (int i = 0; i < listCount; i++) { var item = oListitemCollection[i]; // In order to prevent changes in all places inside the [for loop] item.DeleteObject(); listCount--; // Here, as I am removing 1 item, I am decrementing the count i--; // Executes the clientContext.ExecuteQuery() inside the loop to prevent the // 'The request uses too many resources' error. selectedList.Update(); SPOnlineContext.ExecuteQuery(); //MessageBox.Show("Deletion in Progress ..."); } selectedList.Update(); SPOnlineContext.ExecuteQuery(); //int endCount = oListitemCollection.Count; break; }// END Switch } MessageBox.Show("Deletion Completed!"); // Populate DGV - Refresh!! PopulateDGV(); } else { // SP ON PREM Selections SPOnPremContext = new ClientContext(cboSiteURL.SelectedItem.ToString()); SP.List selectedList = SPOnPremContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @""; ListItemCollection oListitemCollection = selectedList.GetItems(camlQuery); SPOnPremContext.Load(oListitemCollection); SPOnPremContext.ExecuteQuery(); int listCount = oListitemCollection.Count; foreach (ListItem sourceListItem in oListitemCollection) { SP.List destinationList = SPOnPremContext.Web.Lists.GetByTitle(cboSiteList.SelectedItem.ToString()); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem destinationListItem = destinationList.AddItem(itemCreateInfo); switch (cboSiteList.SelectedItem.ToString()) { case "CADD Office Leave Calendar": for (int i = 0; i < listCount; i++) { var item = oListitemCollection[i]; // In order to prevent changes in all places inside the [for loop] item.DeleteObject(); listCount--; // Here, as I am removing 1 item, I am decrementing the count i--; // Executes the clientContext.ExecuteQuery() inside the loop to prevent the // 'The request uses too many resources' error. selectedList.Update(); SPOnPremContext.ExecuteQuery(); //MessageBox.Show("Deletion in Progress ..."); //TODO: Need Progressbar here } selectedList.Update(); SPOnPremContext.ExecuteQuery(); //int endCount = oListitemCollection.Count; break; }// END Switch } MessageBox.Show("Deletion Completed!"); // Populate DGV - Refresh!! PopulateDGV(); } } catch (Exception ex) { ex.Message.ToString(); MessageBox.Show("Error Encountered " + ex.Message); } } }
public void createListwithfields(spClient.ClientContext clientContext, string listName) { try { spClient.ListCreationInformation creationInfo = new spClient.ListCreationInformation(); creationInfo.Title = listName; creationInfo.Description = "This list contains InfoPath library data"; creationInfo.TemplateType = (int)spClient.ListTemplateType.GenericList; spClient.List newList = clientContext.Web.Lists.Add(creationInfo); clientContext.Load(newList); clientContext.ExecuteQuery(); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.ActionPlan + "' Name='" + ASMSRequestConstants.ActionPlan + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.APO + "' Name='" + ASMSRequestConstants.APO + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.APOComments + "' Name='" + ASMSRequestConstants.APOComments + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.AssignedTo + "' Name='" + ASMSRequestConstants.AssignedTo + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='" + ASMSRequestConstants.CaseDate + "' Format='DateOnly' Name='" + ASMSRequestConstants.CaseDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.CaseNo + "' Name='" + ASMSRequestConstants.CaseNo + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.CaseState + "' Name='" + ASMSRequestConstants.CaseState + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.CaseStatus + "' Name='" + ASMSRequestConstants.CaseStatus + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.CaseURL + "' Name='" + ASMSRequestConstants.CaseURL + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Cost + "' Name='" + ASMSRequestConstants.Cost + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.DisapprovedReason + "' Name='" + ASMSRequestConstants.DisapprovedReason + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='" + ASMSRequestConstants.DOC + "' Format='DateOnly' Name='" + ASMSRequestConstants.DOC + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='" + ASMSRequestConstants.ImplementedDate + "' Format='DateOnly' Name='" + ASMSRequestConstants.ImplementedDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.IsApproved + "' Name='" + ASMSRequestConstants.IsApproved + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.IsConfident + "' Name='" + ASMSRequestConstants.IsConfident + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.IsNewCase + "' Name='" + ASMSRequestConstants.IsNewCase + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.IsVerified + "' Name='" + ASMSRequestConstants.IsVerified + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.mComments + "' Name='" + ASMSRequestConstants.mComments + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.NinetyDay + "' Name='" + ASMSRequestConstants.NinetyDay + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Notes + "' Name='" + ASMSRequestConstants.Notes + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.NotRechecked + "' Name='" + ASMSRequestConstants.NotRechecked + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.ParentCase + "' Name='" + ASMSRequestConstants.ParentCase + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.PrjtNo + "' Name='" + ASMSRequestConstants.PrjtNo + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Recheck + "' Name='" + ASMSRequestConstants.Recheck + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.RecheckComments + "' Name='" + ASMSRequestConstants.RecheckComments + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.RecheckDate + "' Name='" + ASMSRequestConstants.RecheckDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.ReportedBy + "' Name='" + ASMSRequestConstants.ReportedBy + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.ReqDesc + "' Name='" + ASMSRequestConstants.ReqDesc + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.RequestedBy + "' Name='" + ASMSRequestConstants.RequestedBy + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Requester + "' Name='" + ASMSRequestConstants.Requester + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='" + ASMSRequestConstants.ResolutionDueDate + "' Format='DateOnly' Name='" + ASMSRequestConstants.ResolutionDueDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.StatusDate + "' Name='" + ASMSRequestConstants.StatusDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Summary + "' Name='" + ASMSRequestConstants.Summary + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.Urgency + "' Name='" + ASMSRequestConstants.Urgency + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.VerifiedDate + "' Name='" + ASMSRequestConstants.VerifiedDate + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.VerifiedReason + "' Name='" + ASMSRequestConstants.VerifiedReason + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.WFState + "' Name='" + ASMSRequestConstants.WFState + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.CreatedBy + "' Name='" + ASMSRequestConstants.CreatedBy + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.ModifiedBy + "' Name='" + ASMSRequestConstants.ModifiedBy + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); newList.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='" + ASMSRequestConstants.AMSRequestWorkflow + "' Name='" + ASMSRequestConstants.AMSRequestWorkflow + "' />", true, spClient.AddFieldOptions.AddToDefaultContentType); clientContext.Load(newList); clientContext.ExecuteQuery(); newList.Update(); } catch (Exception ex) { MessageBox.Show(ex.Message); streamWriter.WriteLine("\r\n\r\n----------------------------- Error -------------------------------"); streamWriter.WriteLine("Error Message :" + ex.Message); streamWriter.WriteLine("Stack Trace :" + ex.StackTrace); } }
/// <inheritdoc/> public void CreateList(string listName, string description, List <string> fieldsAsXml) { // Need to modify to set privilege. using (var context = GetSharepointSiteContext()) { Web orcaSite = context.Web; ListCreationInformation listCreationInfo = new ListCreationInformation(); listCreationInfo.Title = listName; listCreationInfo.TemplateType = (int)ListTemplateType.GenericList; listCreationInfo.Description = description; listCreationInfo.QuickLaunchOption = QuickLaunchOptions.On; // The new list is displayed on the Quick Launch of the site. List catalogList = orcaSite.Lists.Add(listCreationInfo); foreach (var fieldXml in fieldsAsXml) { catalogList.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue); } // Hide the default title field (column). // When created the list, do not set the title as 'required'. Field title = orcaSite.Lists.GetByTitle(listName).Fields.GetByTitle("Title"); title.Hidden = true; title.Required = false; title.Update(); var defaultListView = catalogList.DefaultView; context.Load(defaultListView); context.Load(defaultListView.ViewFields); context.Load(catalogList, listInfo => listInfo.DefaultViewUrl); context.ExecuteQuery(); defaultListView.ViewFields.Remove("LinkTitle"); defaultListView.Update(); // add a navigation link to the newly created list string baseSharepointUrl = _sharepointUrl.Substring(0, _sharepointUrl.IndexOf("/sites")); orcaSite.AddNavigationNode( listName, new Uri($"{baseSharepointUrl}{catalogList.DefaultViewUrl}"), string.Empty, PnP.Framework.Enums.NavigationType.QuickLaunch); // Change permissions. Microsoft.SharePoint.Client.List targetList = orcaSite.Lists.GetByTitle(listName); context.Load( targetList, listInfo1 => listInfo1.HasUniqueRoleAssignments, listInfo2 => listInfo2.RoleAssignments.Include(roles => roles.Member)); context.ExecuteQuery(); context.Load(orcaSite.AssociatedOwnerGroup, group => group.Id); context.ExecuteQuery(); var siteOwnerGroupId = orcaSite.AssociatedOwnerGroup.Id; if (!targetList.HasUniqueRoleAssignments) { // Target list is inheriting role assignments, break that. targetList.BreakRoleInheritance(true, false); } // Delete all exist permissions except admin group. foreach (var assignment in targetList.RoleAssignments) { // Delete the role assignment if it doesn't correspond to the site owners group. if (assignment.Member.PrincipalType == PrincipalType.SharePointGroup && assignment.Member.Id != siteOwnerGroupId) { targetList.RoleAssignments.GetByPrincipal(assignment.Member).DeleteObject(); } } targetList.Update(); context.ExecuteQuery(); Console.WriteLine("List has been created and permissions on it has been modified."); // Currently only site admin can view this list. } }