private void AddViews(frm_Data_View viewForm, string listId, string siteAddress, string siteName) { Guid g = new Guid(listId); SP.ClientContext clientContext = SharePoint.GetClient(siteAddress, frm_Main_Menu.username, frm_Main_Menu.password); SP.List oList = clientContext.Web.Lists.GetById(g); // Load in the Views clientContext.Load(oList.Views); clientContext.ExecuteQuery(); int i = 0; foreach (SP.View oView in oList.Views) { i++; clientContext.Load(oView.ViewFields); clientContext.ExecuteQuery(); string viewName = oView.Title; string viewId = oView.Id.ToString(); string fieldCount = oView.ViewFields.Count.ToString(); string rowLimit = oView.RowLimit.ToString(); string viewQuery = oView.ViewQuery; string url = frm_Main_Menu.siteUrl + oView.ServerRelativeUrl; viewForm.AddRow(i, viewName, siteName, siteAddress, fieldCount, rowLimit, viewId, viewQuery, url); lbl_Row_Count.Text = i.ToString() + " record(s) found"; lbl_Row_Count.Refresh(); } }
private void cmd_Open_List_Click(object sender, EventArgs e) { lbl_Summary.Text = ""; dgv_List.Rows.Clear(); dgv_List.Columns.Clear(); dgv_List.Refresh(); cmd_Versions.Enabled = false; string url = this.txt_Url.Text; string guid = this.txt_Guid.Text; string errorMessage = null; // Get client clientContext = SharePoint.GetClient(url, frm_Main_Menu.username, frm_Main_Menu.password, ref errorMessage); if (clientContext == null) { MessageBox.Show(errorMessage); return; } // Get the SharePoint web Web web = clientContext.Web; try { Guid g = new Guid(guid); oList = web.Lists.GetById(g); } catch (System.FormatException fex) { MessageBox.Show(fex.Message, "Incorrect GUID Format"); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // Load list clientContext.Load(oList); // Execute the query to the server try { clientContext.ExecuteQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } BindList(oList.Fields); }
private static ClientContext GetClient() { string errorMessage = ""; // Get client ClientContext clientContext = SharePoint.GetClient(siteUrl, username, password, ref errorMessage); if (clientContext == null) { MessageBox.Show(errorMessage); return(null); } return(clientContext); }
private void GetListByNameOrGuid(string listGuid, string listName) { txt_Guid.Text = ""; txt_Name.Text = ""; // Store loop counter for rows added to the data grid view int i = 0; // If we find a GUID, there will only be one bool foundGuid = false; // Store variables for field values string defaultViewUrl = null; string defaultViewTitle = null; string viewCount = null; string fieldCount = null; string itemCount = null; string listType = null; string siteName = null; string siteAddress = null; string listTitle = null; // Open the List form frm_Data_List listForm = OpenForm("", "", this.Name, 1000, 500); // Show the Site Name column listForm.dgv_Data.Columns["siteName"].Visible = true; // Hide the Site Address column listForm.dgv_Data.Columns["siteAddress"].Visible = false; // Loop through each row of the data grid view foreach (DataGridViewRow row in dgv_Data.Rows) { // If we find a GUID, there will only be one foundGuid = false; // Store the Site URL to get the Client Context string siteUrl = row.Cells[5].Value.ToString(); // Select the row in the data grid view for visual effect row.Selected = true; // Do this or it won't show the selected record dgv_Data.Refresh(); // Get the Client Context ClientContext clientContext = SharePoint.GetClient(siteUrl, frm_Main_Menu.username, frm_Main_Menu.password); // Store the lists ListCollection collList = clientContext.Web.Lists; // Load lists clientContext.Load(collList); // Execute the query to the server clientContext.ExecuteQuery(); // Loop through each list to check against Name (i.e. Title) or GUID foreach (SP.List oList in collList) { // Variable to check if we need to add a row bool addRow = false; // If we have a GUID, check the GUID if (listGuid != "") { // Check the GUID if (oList.Id.ToString() == listGuid) { // Select the cell to indicate a find row.Cells[1].Selected = true; // Set the variables foundGuid = true; addRow = true; } } // We are checking name else { // Do a contains for basic "fuzzy" matching if (oList.Title.ToLower().Contains(listName.ToLower())) { // Select the cell to indicate a find row.Cells[1].Selected = true; // Set the variable to add a row addRow = true; } } // Check if we are needing to add a row to the data grid view if (addRow) { // Increment counter i++; // Load lists clientContext.Load(oList, l => l.SchemaXml); // Execute the query to the server clientContext.ExecuteQuery(); // Set the field-related vairables SetFieldVars(clientContext, oList, listForm.dgv_Data, ref fieldCount); // Set the view-related variables SetViewVars(clientContext, oList, listForm.dgv_Data, ref defaultViewUrl, ref defaultViewTitle, ref viewCount); // Set other variables itemCount = oList.ItemCount.ToString(); listType = GetBaseTypeDescription(oList.BaseType); siteAddress = row.Cells[5].Value.ToString(); listTitle = oList.Title; // Find the full Site Name that the List belongs to if (row.Cells[2].Value != null) { siteName = row.Cells[2].Value.ToString() + " --> " + row.Cells[1].Value.ToString(); } else { siteName = row.Cells[1].Value.ToString(); } // Add a row to the data grid view listForm.AddRow ( i.ToString(), siteName, siteAddress, listTitle, oList.Description, listType, oList.EnableVersioning, oList.MajorVersionLimit, defaultViewTitle, fieldCount, viewCount, itemCount, !oList.DisableGridEditing, oList.Id.ToString(), oList.Created.ToString(), defaultViewUrl, oList.SchemaXml.ToString() ); lbl_Row_Count.Text = i.ToString() + " record(s) found"; lbl_Row_Count.Refresh(); // Show the form if it is still invisible if (listForm.Visible == false) { listForm.Show(); } // Refresh it for visual effects listForm.Refresh(); // Leave if the addition was the result of a GUID match if (foundGuid == true) { break; } } } // Leave if the addition was the result of a GUID match if (foundGuid == true) { break; } } if (i > 0) { if (foundGuid == true) { MessageBox.Show("Found list '" + listTitle + "' with GUID " + listGuid); } else { MessageBox.Show("Found " + i + " list(s) matching the name '" + listName + "'"); } } else { MessageBox.Show("Could not find a match"); } }
private void AddUsers(string siteName, string siteUrl, frm_Data_User userForm) { ClientContext clientContext = SharePoint.GetClient(siteUrl, frm_Main_Menu.username, frm_Main_Menu.password); Web web = clientContext.Web; // Instantiates the User Information List SP.List userList = web.SiteUserInfoList; // Get the current user SP.User user = web.CurrentUser; // Initialise row counter int rowNum = 0; // Create a new Caml Query CamlQuery camlQuery = new CamlQuery(); // Set the XML camlQuery.ViewXml = "<View><Query>{0}</Query></View>"; // Define a collection to store the list items in ListItemCollection collListItem = userList.GetItems(camlQuery); // Load in the items clientContext.Load(collListItem, icol => icol.Include(i => i.ContentType)); clientContext.Load(collListItem); // Attempt to retreive the List Items try { clientContext.ExecuteQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } foreach (SP.ListItem oListItem in collListItem) { // Increment counter rowNum++; int id = (int)oListItem.FieldValues["ID"]; object firstName = oListItem.FieldValues["FirstName"]; object lastName = oListItem.FieldValues["LastName"]; object fullName = oListItem.FieldValues["Title"]; object userName = oListItem.FieldValues["UserName"]; object email = oListItem.FieldValues["EMail"]; object sipAddress = oListItem.FieldValues["SipAddress"]; object isSiteAdmin = oListItem.FieldValues["IsSiteAdmin"]; object deleted = oListItem.FieldValues["Deleted"]; object hidden = oListItem.FieldValues["UserInfoHidden"]; object isActive = oListItem.FieldValues["IsActive"]; string ContentTypeName = oListItem.ContentType.Name; object guid = oListItem.FieldValues["GUID"]; DateTime createDate = Convert.ToDateTime(oListItem.FieldValues["Created"]); SP.FieldUserValue author = (SP.FieldUserValue)oListItem.FieldValues["Author"]; string createUser = author.LookupValue.ToString(); DateTime modifyDate = Convert.ToDateTime(oListItem.FieldValues["Modified"]); SP.FieldUserValue editor = (SP.FieldUserValue)oListItem.FieldValues["Editor"]; string modifyUser = editor.LookupValue.ToString(); if (firstName != null) { //System.Diagnostics.Debugger.Break(); } userForm.AddRow ( rowNum, id, firstName, lastName, fullName, userName, email, sipAddress, isSiteAdmin, deleted, hidden, isActive, ContentTypeName, guid, createDate, createUser, modifyDate, modifyUser ); lbl_Row_Count.Text = rowNum.ToString() + " record(s) found"; lbl_Row_Count.Refresh(); if (rowNum >= nud_Row_Limit.Value && nud_Row_Limit.Value != 0) { break; } } }
private void AddLists(string siteName, string siteUrl, frm_Data_List listForm) { ClientContext clientContext = SharePoint.GetClient(siteUrl, frm_Main_Menu.username, frm_Main_Menu.password); ListCollection collList = SharePoint.GetLists(clientContext); // Initialise row counter int i = 0; foreach (SP.List oList in collList) { // Load lists clientContext.Load(oList, l => l.SchemaXml); // Execute the query to the server clientContext.ExecuteQuery(); string defaultViewUrl = null; string defaultViewTitle = null; string viewCount = null; string fieldCount = null; string itemCount = oList.ItemCount.ToString(); string listType = GetBaseTypeDescription(oList.BaseType); SetFieldVars(clientContext, oList, listForm.dgv_Data, ref fieldCount); SetViewVars(clientContext, oList, listForm.dgv_Data, ref defaultViewUrl, ref defaultViewTitle, ref viewCount); // Increment counter i++; //oList.DisableGridEditing = (listType == "List"); //oList.Update(); listForm.AddRow ( i.ToString(), siteName, siteUrl, oList.Title, oList.Description, listType, oList.EnableVersioning, oList.MajorVersionLimit, defaultViewTitle, fieldCount, viewCount, itemCount, !oList.DisableGridEditing, oList.Id.ToString(), oList.Created.ToString(), defaultViewUrl, oList.SchemaXml.ToString() ); lbl_Row_Count.Text = i.ToString() + " record(s) found"; lbl_Row_Count.Refresh(); if (i >= nud_Row_Limit.Value && nud_Row_Limit.Value != 0) { break; } } //clientContext.ExecuteQuery(); }
private void AddFields(frm_Data_Field fieldForm, string listId, string siteAddress) { Guid g = new Guid(listId); SP.ClientContext clientContext = SharePoint.GetClient(siteAddress, frm_Main_Menu.username, frm_Main_Menu.password); SP.List oList = clientContext.Web.Lists.GetById(g); // Load in the Fields clientContext.Load(oList.Fields); clientContext.ExecuteQuery(); int i = 0; foreach (SP.Field oField in oList.Fields) { // Store the Field Type string fieldType = oField.TypeAsString; // We will exclude some entries by Field Type bool skip = false; // These 2 attributes will depend on the field type string maxLength = null; string formula = null; // Find field types for specific actions switch (fieldType) { // Max Length case "Text": // Convert to "Text Field" SP.FieldText textField = (SP.FieldText)oField; // We now have access to the attribute maxLength = textField.MaxLength.ToString(); // Break out of the switch break; // Formula case "Calculated": // Convert to "Calculated Field" FieldCalculated calcField = (FieldCalculated)oField; // We now have access to the attribute formula = calcField.Formula; // Break out of the switch break; // We won't include "Computed" or "Lookup" fields case "Lookup": case "Computed": // Skip skip = true; // Break out of the switch break; } if (skip == false) //if (true) { i++; string fieldName = oField.Title; string defaultValue = oField.DefaultValue; string enforceUniqueValues = oField.EnforceUniqueValues.ToString(); string required = oField.Required.ToString(); string readOnly = oField.ReadOnlyField.ToString(); string isSealed = oField.Sealed.ToString(); fieldForm.AddRow ( i, fieldName, oField.InternalName, fieldType, maxLength, enforceUniqueValues, required, readOnly, isSealed, defaultValue, formula ); lbl_Row_Count.Text = i.ToString() + " record(s) found"; lbl_Row_Count.Refresh(); } } }