static void CreateExpenseSiteColumns() { fldExpenseCategory = clientContext.CastTo <FieldChoice>(CreateSiteColumn("ExpenseCategory", "Expense Category", "Choice")); string[] choicesExpenseCategory = ExpenseCategory.GetAll(); fldExpenseCategory.Choices = choicesExpenseCategory; fldExpenseCategory.Update(); clientContext.ExecuteQuery(); fldExpenseDate = clientContext.CastTo <FieldDateTime>(CreateSiteColumn("ExpenseDate", "Expense Date", "DateTime"));; fldExpenseDate.DisplayFormat = DateTimeFieldFormatType.DateOnly; fldExpenseDate.Update(); fldExpenseAmount = clientContext.CastTo <FieldCurrency>(CreateSiteColumn("ExpenseAmount", "Expense Amount", "Currency")); fldExpenseAmount.MinimumValue = 0; fldExpenseBudgetYear = clientContext.CastTo <FieldText>(CreateSiteColumn("ExpenseBudgetYear", "Budget Year", "Text")); fldExpenseBudgetQuarter = clientContext.CastTo <FieldText>(CreateSiteColumn("ExpenseBudgetQuarter", "Budget Quarter", "Text")); fldExpenseBudgetQuarter.Update(); fldExpenseBudgetAmount = clientContext.CastTo <FieldCurrency>(CreateSiteColumn("ExpenseBudgetAmount", "Budget Amount", "Currency")); clientContext.ExecuteQuery(); }
private static Field CreateChoiceField(FieldType fType, string displayName, string internalName, string fieldGroup, IEnumerable <KeyValuePair <string, string> > additionalAttributes, bool addToDefaultView, bool required, string[] choices) { Field field = null; if (web.FieldExistsByName(internalName)) { field = web.Fields.GetByInternalNameOrTitle(internalName); field.DeleteObject(); context.ExecuteQuery(); } FieldCreationInformation fieldCi = new FieldCreationInformation(FieldType.Choice) { DisplayName = displayName, InternalName = internalName, AddToDefaultView = addToDefaultView, Required = required, Id = Guid.NewGuid(), Group = fieldGroup, AdditionalAttributes = additionalAttributes, }; field = web.CreateField(fieldCi); FieldChoice fieldChoice = context.CastTo <FieldChoice>(web.Fields.GetByTitle(displayName)); context.Load(fieldChoice); context.ExecuteQuery(); fieldChoice.Choices = choices; fieldChoice.Update(); context.Load(fieldChoice); context.ExecuteQuery(); return(fieldChoice); }
/// <summary> /// Adds the role. /// </summary> /// <param name="role">The role.</param> /// <returns></returns> public ActionStatus AddRole(string role) { lock (Padlock) { ActionStatus status = new ActionStatus(); try { if (!string.IsNullOrWhiteSpace(role)) { List <NameValueData> roleMasterData = this.GetAllRoles(); if (roleMasterData.Where(m => m.Value.ToUpper().Trim() == role.ToUpper().Trim()).Any()) { status.IsSucceed = false; status.Messages.Add("This Role is already exists."); return(status); } else { List roleMaster = this.web.Lists.GetByTitle(Masters.ROLEMASTER); ListItemCreationInformation info = new ListItemCreationInformation(); ListItem listItem = roleMaster.AddItem(info); listItem["Role"] = role; listItem.Update(); this.context.ExecuteQuery(); List emplist = this.web.Lists.GetByTitle(Masters.APPROVERMASTER); Field rolefield = emplist.Fields.GetByTitle("Role"); FieldChoice fieldChoice = this.context.CastTo <FieldChoice>(rolefield); this.context.Load(fieldChoice); this.context.ExecuteQuery(); List <string> options = new List <string>(fieldChoice.Choices); options.Add(role); fieldChoice.Choices = options.ToArray(); fieldChoice.Update(); this.context.ExecuteQuery(); // this.CreateSPGroup(role); status.IsSucceed = true; status.ExtraData = role; status.Messages.Add("Role added Successfully."); } } } catch (Exception ex) { status.IsSucceed = false; status.Messages.Add("Sorry! Error while Adding Role."); Logger.Error("Error while adding role name =" + role + " Message =" + ex.Message + " StackTrace = " + ex.StackTrace); } return(status); } }
public static void CreateListColumn() { var clientContext = Helper.GetClientContext(); List oList = clientContext.Web.Lists.GetByTitle(listName); //Number DataType Column Field numberField = oList.Fields.AddFieldAsXml("<Field DisplayName='Age' Type='Number' />", true, AddFieldOptions.DefaultValue); FieldNumber fieldNumber = clientContext.CastTo <FieldNumber>(numberField); fieldNumber.MaximumValue = 100; fieldNumber.MinimumValue = 35; fieldNumber.Update(); clientContext.Load(fieldNumber); // //Single Line Of Text DataType Column Field textField = oList.Fields.AddFieldAsXml("<Field DisplayName='SingleLine' Type='Text' />", true, AddFieldOptions.DefaultValue); FieldText fieldText = clientContext.CastTo <FieldText>(textField); fieldText.Update(); clientContext.Load(fieldText); //Multi Line Of Text DataType Column Field multiLineField = oList.Fields.AddFieldAsXml("<Field DisplayName='MultiLine' Type='Note' />", true, AddFieldOptions.DefaultValue); FieldMultiLineText fieldmultiLineText = clientContext.CastTo <FieldMultiLineText>(multiLineField); fieldmultiLineText.Update(); clientContext.Load(fieldmultiLineText); //Multi Line Rich Text DataType Column Field multiLineRichTextField = oList.Fields.AddFieldAsXml("<Field DisplayName='Multi Line RichText' Type='Note' />", true, AddFieldOptions.DefaultValue); FieldMultiLineText fieldmultiLineRichText = clientContext.CastTo <FieldMultiLineText>(multiLineRichTextField); fieldmultiLineRichText.AllowHyperlink = true; fieldmultiLineRichText.RichText = true; fieldmultiLineRichText.Update(); fieldmultiLineRichText.UpdateAndPushChanges(true); clientContext.Load(fieldmultiLineRichText); //An enhanced multi line text field string schemaRichTextField = "<Field Type='Note' Name='EnhancedmultiLine' StaticName='EnhancedmultiLine' DisplayName = 'Enhanced multiLine' NumLines = '6' RichText = 'TRUE' RichTextMode = 'FullHtml' IsolateStyles = 'TRUE' Sortable = 'FALSE' /> "; Field multilineenhancedTextField = oList.Fields.AddFieldAsXml(schemaRichTextField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(multilineenhancedTextField); // DropDown Choice string schemaChoiceFieldDDL = "<Field Type='Choice' DisplayName='ChoiceDDL' Name='ChoiceDDL' StaticName='ChoiceDDL' Format = 'Dropdown' >" + "<Default>Option 2</Default>" + "<CHOICES>" + " <CHOICE>Option 2</CHOICE>" + " <CHOICE>Option 3</CHOICE>" + "</CHOICES>" + "</Field>"; Field choiceDDLField = oList.Fields.AddFieldAsXml(schemaChoiceFieldDDL, true, AddFieldOptions.AddFieldInternalNameHint); FieldChoice fieldChoice = clientContext.CastTo <FieldChoice>(choiceDDLField); fieldChoice.Required = true; fieldChoice.Update(); clientContext.Load(fieldChoice); //Radio buttons string schemaRadioChoiceField = "<Field Type='Choice' Name='ChoiceRadio' StaticName='ChoiceRadio' DisplayName = 'Choice Radio' Format = 'RadioButtons' > " + "<Default>Opt Radio 3</Default>" + "<CHOICES>" + " <CHOICE>Opt Radio 1</CHOICE>" + " <CHOICE>Opt Radio 2</CHOICE>" + " <CHOICE>Opt Radio 3</CHOICE>" + "</CHOICES>" + "</Field>"; Field choiceField = oList.Fields.AddFieldAsXml(schemaRadioChoiceField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(choiceField); //Checkboxes string schemaMultiChoiceField = "<Field Type='MultiChoice' Name='ChoiceMulti' StaticName='ChoiceMulti' DisplayName = 'Choice Multi' > " + "<Default>MultiChoice 2</Default>" + "<CHOICES>" + " <CHOICE>MultiChoice 1</CHOICE>" + " <CHOICE>MultiChoice 2</CHOICE>" + " <CHOICE>MultiChoice 3</CHOICE>" + "</CHOICES>" + "</Field>"; Field choiceMultiChoiceField = oList.Fields.AddFieldAsXml(schemaMultiChoiceField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(choiceMultiChoiceField); //Fill In option string schemaFillInChoiceField = "<Field Type='Choice' DisplayName='Fill In Choice' Name='FillInChoice' StaticName='FillInChoice' Format = 'Dropdown' FillInChoice = 'TRUE' > " + "<Default>My Choices Data will come here</Default>" + "<CHOICES>" + " <CHOICE>FillInChoice 1</CHOICE>" + " <CHOICE>FillInChoice 2</CHOICE>" + " <CHOICE>FillInChoice 3</CHOICE>" + "</CHOICES>" + "</Field>"; Field choiceFillInChoiceField = oList.Fields.AddFieldAsXml(schemaFillInChoiceField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(choiceFillInChoiceField); //Picture field string schemaPictureField = "<Field Type='URL' Name='EmployeePicture' StaticName='EmployeePicture' DisplayName = 'Employee Picture' Format = 'Image' /> "; Field pictureField = oList.Fields.AddFieldAsXml(schemaPictureField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(pictureField); //URL field string schemaUrlField = "<Field Type='URL' Name='BlogUrl' StaticName='BlogUrl' DisplayName='Blog URL' Format='Hyperlink'/>"; Field urlField = oList.Fields.AddFieldAsXml(schemaUrlField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(urlField); clientContext.ExecuteQuery(); // Lookup field List countryList = clientContext.Web.Lists.GetByTitle("Countries"); clientContext.Load(countryList, c => c.Id); clientContext.ExecuteQuery(); // //define the relationship with the lookup field, in that case the field needs to be indexed: // string schemaLookupField = "<Field Type='Lookup' Name='Country' StaticName='Country' DisplayName='Country Name' List = '{B5E2D800F-E739-401F-983F-B40984B70273}' ShowField = 'Title' RelationshipDeleteBehavior = 'Restrict' Indexed = 'TRUE' /> "; //string schemaLookupField = "<Field Type='Lookup' Name='Country' StaticName='Country' DisplayName='Country Name' List = 'Countries' ShowField = 'Title' RelationshipDeleteBehavior = 'Restrict' Indexed = 'TRUE' /> "; string schemaLookupField = "<Field Type='Lookup' Name='Country' StaticName='Country' DisplayName='Country Name' List = '" + countryList.Id + "' ShowField = 'Title' /> "; Field lookupField = oList.Fields.AddFieldAsXml(schemaLookupField, true, AddFieldOptions.AddFieldInternalNameHint); lookupField.Update(); clientContext.Load(lookupField); //// multi-select lookup field string schemaMultiLookupField = "<Field Type='LookupMulti' Name='Country' StaticName='Country' DisplayName='Country' List = '" + countryList.Id + "' ShowField = 'Title' Mult = 'TRUE' /> "; //string schemaMultiLookupField = "<Field Type='LookupMulti' Name='Country' StaticName='Country' DisplayName='Country' List = 'Countries' ShowField = 'Title' Mult = 'TRUE' /> "; Field lookupFieldmulti = oList.Fields.AddFieldAsXml(schemaMultiLookupField, true, AddFieldOptions.AddFieldInternalNameHint); lookupFieldmulti.Update(); clientContext.Load(lookupFieldmulti); ////Ref: https://karinebosch.wordpress.com/my-articles/creating-fields-using-csom/ // //User Field string schemaUserField = "<Field Type='User' Name='UserName' StaticName='UserName' DisplayName='User Name' />"; Field userField = oList.Fields.AddFieldAsXml(schemaUserField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(userField); ////User Field multiple //string schemaUserGroupField = "<Field Type='UserMulti' Name='EmployeeName' StaticName='EmployeeName' DisplayName='Employee Name' UserSelectionMode = 'PeopleOnly' UserSelectionScope = '7' Mult = 'TRUE' /> "; string schemaUserGroupField = "<Field Type='UserMulti' Name='EmployeeName' StaticName='EmployeeName' DisplayName='Employee Name' UserSelectionMode = 'PeopleAndGroups' UserSelectionScope = '7' Mult = 'TRUE' /> "; Field userGroupField = oList.Fields.AddFieldAsXml(schemaUserGroupField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(userGroupField); ////boolean field string schemaBooleanField = "<Field Type='Boolean' Name='Married' StaticName='Married' DisplayName='Married'> <Default>1</Default> </Field>"; Field booleanField = oList.Fields.AddFieldAsXml(schemaBooleanField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(booleanField); ////DateTime Field //Date only field string schemaBirthDate = "<Field Type='DateTime' Name='BirthDate' StaticName='BirthDate' DisplayName = 'Birth date' Format = 'DateOnly'> <Default>[Today]</Default></Field>"; Field birthDateField = oList.Fields.AddFieldAsXml(schemaBirthDate, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(birthDateField); ////Date and time field string schemaArrivalField = "<Field Type='DateTime' Name='ArrivalDateTime' StaticName='ArrivalDateTime' DisplayName = 'Arrival' Format = 'DateTime'> <Default>[Now]</Default></Field>"; Field DateTimeField = oList.Fields.AddFieldAsXml(schemaArrivalField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(DateTimeField); ////hidden field string schemaHiddenTextField = "<Field Type='Text' Name='HiddenField' StaticName='HiddenField' DisplayName='Hidden Field' Hidden='TRUE' />"; Field hiddenTextField = oList.Fields.AddFieldAsXml(schemaHiddenTextField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(hiddenTextField); //indexed field // Not Working as of now //Field f = oList.Fields.GetByInternalNameOrTitle("ID"); //clientContext.Load(f); //clientContext.ExecuteQuery(); //f.Indexed = true; //f.Update(); //Managed Metadata field Guid termStoreId = Guid.Empty; Guid termSetId = Guid.Empty; GetTaxonomyFieldInfo(clientContext, out termStoreId, out termSetId); // Single selection Taxonomy field string schemaTaxonomyField = "<Field Type='TaxonomyFieldType' Name='TaxonomyField' StaticName='TaxonomyField' DisplayName = 'Taxonomy Field' /> "; Field taxonomyFieldSingle = oList.Fields.AddFieldAsXml(schemaTaxonomyField, true, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(taxonomyFieldSingle); // Retrieve the field as a Taxonomy Field TaxonomyField taxonomyField = clientContext.CastTo <TaxonomyField>(taxonomyFieldSingle); taxonomyField.SspId = termStoreId; taxonomyField.TermSetId = termSetId; taxonomyField.TargetTemplate = String.Empty; taxonomyField.AnchorId = Guid.Empty; taxonomyField.Update(); // Multi selection Taxonomy field string schemaTaxonomyFieldMulti = "<Field Type='TaxonomyFieldTypeMulti' Name='TaxonomyFieldMulti' StaticName='TaxonomyFieldMulti' DisplayName = 'Taxonomy Field Multi' Mult = 'TRUE' /> "; Field taxonomyFieldMulti = oList.Fields.AddFieldAsXml(schemaTaxonomyFieldMulti, false, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(taxonomyFieldMulti); // Retrieve the field as a Taxonomy Field TaxonomyField taxonomyField1 = clientContext.CastTo <TaxonomyField>(taxonomyFieldMulti); taxonomyField1.SspId = termStoreId; taxonomyField1.TermSetId = termSetId; taxonomyField1.TargetTemplate = String.Empty; taxonomyField1.AnchorId = Guid.Empty; taxonomyField1.Update(); clientContext.ExecuteQuery(); //Calculated field // Not Working //string formula = "<Formula>=Age&\"\"&SingleLine&\"(id:\"&ID&\"\"</Formula>" // + "<FieldRefs>" // + "<FieldRef Name='Age' />" // + "<FieldRef Name='SingleLine' />" // + "<FieldRef Name='ID' />" // + "</FieldRefs>"; //string schemaCalculatedField = "<Field Type='Calculated' Name='CalculatedField' StaticName='CalculatedField' DisplayName = 'Calculated Field' ResultType = 'Text' Required = 'TRUE' ReadOnly = 'TRUE' > " + formula + " </ Field > "; //Field fullNameField = oList.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint); //clientContext.ExecuteQuery(); string fieldXml = "<Field Name='CalculatedField_Year' StaticName='CalculatedField_Year' DisplayName='CalculatedField Year' Type='Text' ReadOnly = 'TRUE'>" + "<DefaultFormula>=CONCATENATE(YEAR(Today))</DefaultFormula>" + "</Field>"; Field field = oList.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue); clientContext.ExecuteQuery(); }
public static void CreateCarContetType(ClientContext ctx) { string VehicleContentType = "0x0100567A676ED8534670A79990FADE68DE47"; string carContentType = "0x0100567A676ED8534670A79990FADE68DE4700DC042CAC4BCA4EDFB9334B39999E4576"; if (!ctx.Web.ContentTypeExistsById(VehicleContentType)) { ctx.Web.CreateContentType("Vehicle", VehicleContentType, "Davids Content Types"); } if (!ctx.Web.ContentTypeExistsById(carContentType)) { ctx.Web.CreateContentType("Car", carContentType, "Davids Content Types"); } Guid carModleFieldId = "{6DAD1B3E-A841-4E35-8B00-0E68E269C1F5}".ToGuid(); if (!ctx.Web.FieldExistsById(carModleFieldId)) { FieldCreationInformation modleFieldInfo = new FieldCreationInformation(FieldType.Text); modleFieldInfo.Id = carModleFieldId; modleFieldInfo.InternalName = "F_CarModel"; modleFieldInfo.DisplayName = "Car Model"; modleFieldInfo.Group = "Davids Fields"; ctx.Web.CreateField(modleFieldInfo); } ctx.Web.AddFieldToContentTypeById(carContentType, carModleFieldId.ToString()); Guid carYearFieldId = "{AB76F05D-FC5F-4456-989D-F435BBF39230}".ToGuid(); if (!ctx.Web.FieldExistsById(carYearFieldId)) { FieldCreationInformation yearFieldInfo = new FieldCreationInformation(FieldType.Number); yearFieldInfo.Id = carYearFieldId; yearFieldInfo.InternalName = "F_CarYear"; yearFieldInfo.DisplayName = "Car Year"; yearFieldInfo.Group = "Davids Fields"; ctx.Web.CreateField(yearFieldInfo); } ctx.Web.AddFieldToContentTypeById(carContentType, carYearFieldId.ToString()); Guid carColorFieldId = "{3A2B1887-ABBF-4EDB-BCCC-11CF48A279EA}".ToGuid(); if (!ctx.Web.FieldExistsById(carColorFieldId)) { FieldCreationInformation colorFieldInfo = new FieldCreationInformation(FieldType.Choice); colorFieldInfo.Id = carColorFieldId; colorFieldInfo.InternalName = "F_CarColor"; colorFieldInfo.DisplayName = "Car Color"; colorFieldInfo.Group = "Davids Fields"; FieldChoice field = ctx.Web.CreateField <FieldChoice>(colorFieldInfo); field.Choices = new string[] { "Green", "Blue", "Red" }; field.DefaultValue = "Green"; field.Update(); ctx.ExecuteQuery(); } ctx.Web.AddFieldToContentTypeById(carContentType, carColorFieldId.ToString()); ctx.Web.CreateList(ListTemplateType.GenericList, "Cars", false, true, "lists/cars", true); ctx.Web.AddContentTypeToListById("Cars", carContentType, true); }
static void CreateWingtipSiteColumns() { Console.WriteLine(); Console.WriteLine("Creating site columns"); fieldProductCode = CreateSiteColumn("ProductCode", "Product Code", "Text"); fieldProductCode.EnforceUniqueValues = true; fieldProductCode.Indexed = true; fieldProductCode.Required = true; fieldProductCode.Update(); clientContext.ExecuteQuery(); clientContext.Load(fieldProductCode); clientContext.ExecuteQuery(); fieldProductListPrice = clientContext.CastTo<FieldCurrency>(CreateSiteColumn("ProductListPrice", "List Price", "Currency")); fieldProductCategory = clientContext.CastTo<FieldChoice>(CreateSiteColumn("ProductCategory", "Product Category", "Choice")); string[] choicesProductCategory = { "Action Figures", "Arts and Crafts", "Vehicles and Remote Control" }; fieldProductCategory.Choices = choicesProductCategory; fieldProductCategory.Update(); clientContext.ExecuteQuery(); fieldProductColor = clientContext.CastTo<FieldChoice>(CreateSiteColumn("ProductColor", "Product Color", "Choice")); string[] choicesProductColor = { "White", "Black", "Grey", "Blue", "Red", "Green", "Yellow" }; fieldProductColor.Choices = choicesProductColor; fieldProductColor.Update(); clientContext.ExecuteQuery(); fieldMinimumAge = clientContext.CastTo<FieldNumber>(CreateSiteColumn("MinimumAge", "Minimum Age", "Number")); fieldMaximumAge = clientContext.CastTo<FieldNumber>(CreateSiteColumn("MaximumAge", "Maximum Age", "Number")); }
public static void CreateBookCT(ClientContext ctx) { string bookCT = "0x01000E870749A9444905BB8A362E475B0798"; Web web = ctx.Site.RootWeb; //web.GetListByTitle("Books2").DeleteObject(); //ctx.ExecuteQuery(); //web.DeleteContentTypeById(bookCT); if (!web.ContentTypeExistsById(bookCT)) { web.CreateContentType("David Books", bookCT, "Davids ContentType"); } string bookTypeFieldId = "{DBB24705-0DEA-4C4F-8C2A-95CB6F0DE25E}"; if (!web.FieldExistsById(new Guid(bookTypeFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Choice); info.Id = bookTypeFieldId.ToGuid(); info.InternalName = "DAV_BookType"; info.DisplayName = "Book Type"; info.Group = "Tims Columns"; FieldChoice field = web.CreateField <FieldChoice>(info); field.Choices = new string[] { "Romance", "Drama", "Horror", "Thriller" }; field.Update(); ctx.ExecuteQuery(); } string authorFieldId = "{D6996667-0BEA-4C9F-9904-DEB21CC5AA84}"; if (!web.FieldExistsById(new Guid(authorFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Text); info.Id = authorFieldId.ToGuid(); info.InternalName = "DAV_Author"; info.DisplayName = "Author"; info.Group = "Tims Columns"; Field field = web.CreateField(info); } string releaseDateFieldId = "{84716863-06CA-4D31-BAA0-7D099FC501E7}"; if (!web.FieldExistsById(new Guid(releaseDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.DateTime); info.Id = releaseDateFieldId.ToGuid(); info.InternalName = "DAV_Realesedate"; info.DisplayName = "ReleaseDate"; info.Group = "Tims Columns"; FieldDateTime field = web.CreateField <FieldDateTime>(info); field.DisplayFormat = DateTimeFieldFormatType.DateOnly; field.Update(); ctx.ExecuteQuery(); } string descriptionDateFieldId = "{4BD3F599-4D5C-412D-8431-6ECD36AEB015}"; // web.RemoveFieldById(descriptionDateFieldId); if (!web.FieldExistsById(new Guid(descriptionDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Note); info.Id = descriptionDateFieldId.ToGuid(); info.InternalName = "DAV_description"; info.DisplayName = "Description"; info.Group = "Tims Columns"; info.Required = true; FieldMultiLineText field = web.CreateField <FieldMultiLineText>(info); field.RichText = true; field.NumberOfLines = 10; field.AllowHyperlink = true; field.Update(); ctx.ExecuteQuery(); } web.AddFieldToContentTypeById(bookCT, bookTypeFieldId); web.AddFieldToContentTypeById(bookCT, authorFieldId); web.AddFieldToContentTypeById(bookCT, releaseDateFieldId); web.AddFieldToContentTypeById(bookCT, descriptionDateFieldId, true); if (!web.ListExists("Books2")) { List list = web.CreateList(ListTemplateType.GenericList, "Books2", false, urlPath: "lists/books2", enableContentTypes: true); list.AddContentTypeToListById(bookCT, true); View listView = list.DefaultView; listView.ViewFields.Add("DAV_BookType"); listView.ViewFields.Add("DAV_Author"); listView.ViewFields.Add("DAV_Realesedate"); listView.ViewFields.Add("DAV_description"); listView.Update(); ctx.ExecuteQueryRetry(); } List bookList = web.GetListByTitle("Books2"); ListItem item = bookList.AddItem(new ListItemCreationInformation()); item["Title"] = "MistBorn"; item["DAV_BookType"] = "Fantasy"; item["DAV_Author"] = "Brandon Sanderson"; item["DAV_Realesedate"] = DateTime.Parse("2001-02-12"); item["DAV_description"] = "This is a decription \n\n is this a new line?"; item.Update(); ctx.ExecuteQuery(); //ListItemCollection items = bookList.GetItems(CamlQuery.CreateAllItemsQuery()); //ctx.Load(items); //ctx.ExecuteQuery(); }
///// <summary> ///// Creates the sp group. ///// </summary> ///// <param name="role">The role.</param> //private void CreateSPGroup(string role) //{ // try // { // Group addgroup = null; // try // { // addgroup = this.context.Web.SiteGroups.GetByName(role); // this.context.Load(addgroup); // this.context.ExecuteQuery(); // } // catch // { // Logger.Error(role + " Group not found.Creating Group now."); // addgroup = null; // } // if (addgroup == null) // { // //this.web.BreakRoleInheritance(true, false); // User owner = this.web.EnsureUser("*****@*****.**"); // // User member = this.web.EnsureUser("*****@*****.**"); // GroupCreationInformation groupCreationInfo = new GroupCreationInformation(); // groupCreationInfo.Title = role; // groupCreationInfo.Description = "Group Name : " + role; // Group group = this.web.SiteGroups.Add(groupCreationInfo); // group.Owner = owner; // // group.Users.AddUser(member); // group.Update(); // this.context.ExecuteQuery(); // // Get the Role Definition (Permission Level) // var customFullControlRoleDefinition = this.web.RoleDefinitions.GetByName("Contribute"); // this.context.Load(customFullControlRoleDefinition); // this.context.ExecuteQuery(); // // Add it to the Role Definition Binding Collection // RoleDefinitionBindingCollection collRDB = new RoleDefinitionBindingCollection(this.context); // collRDB.Add(this.web.RoleDefinitions.GetByName("Contribute")); // // Bind the Newly Created Permission Level to the new User Group // this.web.RoleAssignments.Add(group, collRDB); // this.context.Load(group); // this.context.ExecuteQuery(); // } // } // catch (Exception ex) // { // Logger.Error("Error while adding Group name =" + role + " Message =" + ex.Message + " StackTrace = " + ex.StackTrace); // } //} ///// <summary> ///// Deletes the sp group. ///// </summary> ///// <param name="role">The role.</param> //private void DeleteSPGroup(string role) //{ // Group deletegroup = null; // try // { // deletegroup = this.context.Web.SiteGroups.GetByName(role); // this.context.Load(deletegroup); // this.context.ExecuteQuery(); // } // catch // { // Logger.Error(role + " Group could not found in sharepoint Group."); // deletegroup = null; // } // try // { // if (deletegroup != null) // { // this.web.RoleAssignments.GetByPrincipal(deletegroup).DeleteObject(); // this.web.Update(); // this.context.ExecuteQuery(); // GroupCollection groupColl = this.web.SiteGroups; // groupColl.Remove(deletegroup); // this.context.ExecuteQuery(); // } // } // catch (Exception ex) // { // Logger.Error("Error while deleting Group name =" + role + " Message =" + ex.Message + " StackTrace = " + ex.StackTrace); // } //} /// <summary> /// Deletes the role. /// </summary> /// <param name="roleID">The role identifier.</param> /// <returns></returns> public ActionStatus DeleteRole(int roleID) { ActionStatus status = new ActionStatus(); try { string roleName = string.Empty; if (roleID > 0) { List roleMaster = this.web.Lists.GetByTitle(Masters.ROLEMASTER); ListItem item = roleMaster.GetItemById(roleID); this.context.Load(item); this.context.ExecuteQuery(); if (item != null) { roleName = Convert.ToString(item["Role"]); List emplist = this.web.Lists.GetByTitle(Masters.APPROVERMASTER); CamlQuery camlEmpQuery = new CamlQuery(); camlEmpQuery.ViewXml = @"<View> <Query> <Where> <Eq> <FieldRef Name='Role' /> <Value Type='Choice'>" + roleName + @"</Value> </Eq> </Where> </Query> </View>"; ListItemCollection empitems = emplist.GetItems(camlEmpQuery); this.context.Load(empitems); this.context.ExecuteQuery(); if (empitems != null && empitems.Count > 0) { status.IsSucceed = false; status.Messages.Add("You can't delete this role because " + empitems.Count + " employee(s) are already assigned to this role.To delete this role please remove/reassign the other role to employee(s)."); } else if (empitems == null || empitems.Count == 0) { List screenMapMaster = this.web.Lists.GetByTitle(ICCPListNames.ROLESCREENMAPPING); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @"<View> <Query> <Where> <Eq> <FieldRef Name='RoleID' /> <Value Type='Lookup'>" + roleName + @"</Value> </Eq> </Where> </Query> </View>"; ListItemCollection items = screenMapMaster.GetItems(camlQuery); this.context.Load(items); this.context.ExecuteQuery(); if (items != null && items.Count > 0) { foreach (ListItem deleteitem in items.ToList()) { deleteitem.DeleteObject(); } this.context.ExecuteQuery(); } if (items != null && items.Count == 0) { item.DeleteObject(); this.context.ExecuteQuery(); Field rolefield = emplist.Fields.GetByTitle("Role"); FieldChoice fieldChoice = this.context.CastTo <FieldChoice>(rolefield); this.context.Load(fieldChoice); this.context.ExecuteQuery(); List <string> options = new List <string>(fieldChoice.Choices); options.Remove(roleName); fieldChoice.Choices = options.ToArray(); fieldChoice.Update(); this.context.ExecuteQuery(); // this.DeleteSPGroup(Convert.ToString(roleName)); status.IsSucceed = true; status.Messages.Add("Role Deleted Successfully."); } } } } } catch (Exception ex) { status.IsSucceed = true; status.Messages.Add("Sorry! Error while delete Role."); Logger.Error("Error while delete role having roleId =" + roleID + " Message =" + ex.Message + " StackTrace = " + ex.StackTrace); } return(status); }