/// <summary> /// /// </summary> /// <param name="web"></param> /// <param name="group"></param> /// <param name="description"></param> /// <param name="name"></param> /// <param name="displayName"></param> /// <param name="decimals"></param> public static void CreateNumberColumn(this SPWeb web, string group, string description, string name, string displayName, SPNumberFormatTypes decimals = SPNumberFormatTypes.Automatic) { try { if (!web.Fields.ContainsField(name)) { string fieldName = web.Fields.Add(name, SPFieldType.Number, false); SPFieldNumber field = (SPFieldNumber)web.Fields.GetField(name); field.Group = group; field.DisplayFormat = decimals; field.StaticName = name; field.Title = displayName; field.NoCrawl = false; field.Description = description; field.Update(); } } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("CORE:HELPERS", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, String.Format("Exception happened in Helpers:CreateNumberColumn. MESSAGE: {0}. EXCEPTION TRACE: {1} ", ex.Message, ex.StackTrace), ex.StackTrace); } }
private void CheckForQuickLaunchField(SPWeb spWeb) { spWeb.AllowUnsafeUpdates = true; SPList list = spWeb.Lists.TryGetList(INSTALLED_APPS); if (list != null) { if (!list.Fields.ContainsFieldWithInternalName("QuickLaunchOrder")) { SPFieldNumber fldQuickLaunchOrder = (SPFieldNumber)list.Fields.CreateNewField(SPFieldType.Number.ToString(), "QuickLaunchOrder"); fldQuickLaunchOrder.Title = "QuickLaunchOrder"; fldQuickLaunchOrder.DisplayFormat = SPNumberFormatTypes.NoDecimal; fldQuickLaunchOrder.Indexed = false; list.Fields.Add(fldQuickLaunchOrder); list.Update(); fldQuickLaunchOrder = list.Fields["QuickLaunchOrder"] as SPFieldNumber; fldQuickLaunchOrder.Title = "QuickLaunchOrder"; fldQuickLaunchOrder.ShowInListSettings = true; fldQuickLaunchOrder.DefaultValue = "1000"; fldQuickLaunchOrder.ShowInDisplayForm = true; fldQuickLaunchOrder.ShowInNewForm = true; fldQuickLaunchOrder.ShowInEditForm = true; fldQuickLaunchOrder.Hidden = false; fldQuickLaunchOrder.Update(); SPView tdefaultList = list.DefaultView; tdefaultList.ViewFields.Add("QuickLaunchOrder"); tdefaultList.Update(); int index = 0; SPQuery query = new SPQuery(); query.Query = "<Where><IsNull><FieldRef Name='EXTID'/></IsNull></Where><OrderBy><FieldRef Name='Title'/></OrderBy>"; foreach (SPListItem li in list.GetItems(query)) { if (spWeb.ParentWeb == null && li["Title"].ToString().Equals("Global My Workplace", StringComparison.InvariantCultureIgnoreCase)) { li["QuickLaunchOrder"] = "-1"; } else { li["QuickLaunchOrder"] = index++; } li.Update(); } } } spWeb.AllowUnsafeUpdates = false; }
private SPList EnsureGlymaExportsList() { bool isNewList = false; SPList list = Site.EnsureList(GlymaLists.GlymaExportsList, ref isNewList); SPField titleColumn = list.Fields[SPBuiltInFieldId.Title]; titleColumn.Indexed = true; titleColumn.Title = "WorkItem ID"; titleColumn.EnforceUniqueValues = true; titleColumn.Update(); bool isNewColumn = false; SPField exportStatus = list.EnsureColumn(GlymaColumns.ExportStatus, false, ref isNewColumn); exportStatus.Required = true; exportStatus.Update(); isNewColumn = false; SPField exportType = list.EnsureColumn(GlymaColumns.ExportType, false, ref isNewColumn); exportType.Required = true; exportType.Update(); isNewColumn = false; SPField mapType = list.EnsureColumn(GlymaColumns.MapType, false, ref isNewColumn); mapType.Required = true; mapType.Update(); isNewColumn = false; SPField percentageCompletedField = list.EnsureColumn(GlymaColumns.PercentageComplete, false, ref isNewColumn); SPFieldNumber percentComplete = percentageCompletedField as SPFieldNumber; percentComplete.Required = true; percentComplete.ShowAsPercentage = true; percentComplete.MaximumValue = 1; percentComplete.MinimumValue = 0; percentComplete.Update(); isNewColumn = false; SPField domainUid = list.EnsureColumn(GlymaColumns.DomainUid, false, ref isNewColumn); domainUid.Required = true; domainUid.Update(); isNewColumn = false; SPField rootMapUid = list.EnsureColumn(GlymaColumns.RootMapUid, false, ref isNewColumn); rootMapUid.Required = true; rootMapUid.Update(); isNewColumn = false; SPField exportPropertiesField = list.EnsureColumn(GlymaColumns.ExportProperties, false, ref isNewColumn); SPFieldMultiLineText exportProperties = exportPropertiesField as SPFieldMultiLineText; exportProperties.Required = false; exportProperties.AppendOnly = false; exportProperties.RichText = false; exportProperties.Update(); isNewColumn = false; SPField logMessagesField = list.EnsureColumn(GlymaColumns.LogMessages, false, ref isNewColumn); SPFieldMultiLineText logMessages = logMessagesField as SPFieldMultiLineText; logMessages.Required = false; logMessages.AppendOnly = true; logMessages.RichText = false; logMessages.Update(); SPField lastModifiedField = list.Fields[SPBuiltInFieldId.Modified]; // Configure the default view for the list. SPView defaultView = list.DefaultView; defaultView.ViewFields.DeleteAll(); defaultView.ViewFields.Add(titleColumn); defaultView.ViewFields.Add(exportStatus); defaultView.ViewFields.Add(exportType); defaultView.ViewFields.Add(mapType); defaultView.ViewFields.Add(percentComplete); defaultView.ViewFields.Add(domainUid); defaultView.ViewFields.Add(rootMapUid); defaultView.ViewFields.Add(exportProperties); defaultView.ViewFields.Add(logMessages); defaultView.Query = "<OrderBy><FieldRef Name=\"" + lastModifiedField.InternalName + "\" Ascending=\"FALSE\" /></OrderBy>"; defaultView.Update(); return(list); }
public static void AddColForSortToList(string siteUrl) { if (!string.IsNullOrEmpty(siteUrl)) { string colName = "ColForSort"; List <string> listNameCollection = new List <string>() { "Change Shift Management", "Leave Of Absence For Overtime Management", "Vehicle Management", "Leave Management" }; //"Overtime Management", "Overtime Employee Details", try { SPSecurity.RunWithElevatedPrivileges(delegate() { Console.Write("Processing..."); using (SPSite spSite = new SPSite(siteUrl)) { using (SPWeb spWeb = spSite.RootWeb) { #region CalculatedField foreach (var listName in listNameCollection) { SPList spList = spWeb.Lists.TryGetList(listName); if (spList != null) { Console.WriteLine(); Console.Write("List Name: {0}", listName); spWeb.AllowUnsafeUpdates = true; if (spList.Fields.ContainsField(colName) == false) { spList.Fields.Add(colName, SPFieldType.Calculated, false); SPField spField = spList.Fields.TryGetFieldByStaticName(colName); if (spField != null) { SPFieldCalculated calculatedField = spField as SPFieldCalculated; calculatedField.OutputType = SPFieldType.Number; calculatedField.DisplayFormat = SPNumberFormatTypes.NoDecimal; calculatedField.Formula = "=IF([Approval Status]=\"Approved\",INT(\"7\"),IF([Approval Status]=\"Rejected\",INT(\"8\"),IF([Approval Status]=\"Cancelled\",INT(\"9\"),INT(\"0\"))))"; calculatedField.Update(); Console.Write(" -> Done"); } else { Console.Write(" -> Failed"); } } else { Console.Write(" -> Existed"); } spWeb.AllowUnsafeUpdates = false; } else { Console.WriteLine(); Console.Write("Cannot find list: {0}", listName); } } #endregion #region Create ColForSort for Overtime Management List string overtimeManagementListName = "Overtime Management"; Console.WriteLine(); Console.Write("List Name: {0}", overtimeManagementListName); SPList overtimeManagementList = spWeb.Lists.TryGetList(overtimeManagementListName); if (overtimeManagementList != null) { if (overtimeManagementList.Fields.ContainsField(colName) == false) { spWeb.AllowUnsafeUpdates = true; overtimeManagementList.Fields.Add(colName, SPFieldType.Number, false); SPField spField = overtimeManagementList.Fields.TryGetFieldByStaticName(colName); if (spField != null) { SPFieldNumber spFieldNumber = spField as SPFieldNumber; spFieldNumber.DisplayFormat = SPNumberFormatTypes.NoDecimal; spFieldNumber.Update(); Console.Write(" -> Done"); } else { Console.Write(" -> Failed"); } spWeb.AllowUnsafeUpdates = false; } else { Console.Write(" -> Existed"); } } else { Console.WriteLine(); Console.Write("Cannot find list: {0}", overtimeManagementListName); } #endregion #region Create CalculateField ColForSort for Overtime Employee Details list string overtimeDetailsListName = "Overtime Employee Details"; Console.WriteLine(); Console.Write("List Name: {0}", overtimeDetailsListName); SPList overtimeDetailsList = spWeb.Lists.TryGetList(overtimeDetailsListName); SPList overtimeList = spWeb.Lists.TryGetList("Overtime Management"); if (overtimeDetailsList != null && overtimeList != null) { if (overtimeDetailsList.Fields.ContainsField(colName) == false && overtimeList.Fields.ContainsField(colName) == true) { spWeb.AllowUnsafeUpdates = true; overtimeDetailsList.Fields.AddDependentLookup(colName, new Guid("{878e2996-150c-4e63-b632-ba90dec566a0}")); SPField spField = overtimeDetailsList.Fields.TryGetFieldByStaticName(colName); if (spField != null) { SPFieldLookup spFieldLookup = spField as SPFieldLookup; spFieldLookup.LookupField = colName; spFieldLookup.Update(); Console.Write(" -> Done"); } else { Console.Write(" -> Failed"); } spWeb.AllowUnsafeUpdates = false; } else { Console.Write(" -> Existed"); } } #endregion } } }); } catch (Exception ex) { Console.WriteLine(string.Format("Error: {0}", ex.Message)); } Console.WriteLine(); //Console.Write("Press any key to exit..."); //Console.Read(); } else { Console.Write("Troll?"); Console.Read(); } }
//Create Site Columns public void CreateSiteColumns() { using (SPWeb oSPWeb = oSPSite.RootWeb) { try { //if (!oSPWeb.Fields.ContainsField("Codigo")) //{ //Codigo de Documento string codigoField = oSPWeb.Fields.Add("Codigo", SPFieldType.Text, false); SPFieldText Codigo = (SPFieldText)oSPWeb.Fields.GetFieldByInternalName(codigoField); Codigo.Group = groupColumn; Codigo.Update(); //} //if (!oSPWeb.Fields.ContainsField("Referencia")) //{ string referenciaField = oSPWeb.Fields.Add("Referencia", SPFieldType.Text, false); SPFieldText Referencia = (SPFieldText)oSPWeb.Fields.GetFieldByInternalName(referenciaField); Referencia.Group = groupColumn; Referencia.Update(); //} //if (!oSPWeb.Fields.ContainsField("Comentario")) { string comentarioField = oSPWeb.Fields.Add("Comentario", SPFieldType.Text, false); SPFieldText Comentario = (SPFieldText)oSPWeb.Fields.GetFieldByInternalName(comentarioField); Comentario.Group = groupColumn; Comentario.Update(); //} //if (!oSPWeb.Fields.ContainsField("Remitente")) //{ string remitenteField = oSPWeb.Fields.Add("Remitente", SPFieldType.Text, false); SPFieldText Remitente = (SPFieldText)oSPWeb.Fields.GetFieldByInternalName(remitenteField); Remitente.Group = groupColumn; Remitente.Update(); //} //if (!oSPWeb.Fields.ContainsField("Factura")) { string facturaField = oSPWeb.Fields.Add("Factura", SPFieldType.Number, false); SPFieldNumber Factura = (SPFieldNumber)oSPWeb.Fields.GetFieldByInternalName(facturaField); Factura.Group = groupColumn; Factura.Update(); //} //if (!oSPWeb.Fields.ContainsField("Fecha Receopcion")) { string fechaRecepcionField = oSPWeb.Fields.Add("Fecha Recepcion", SPFieldType.DateTime, true); SPFieldDateTime FechaRecepcion = (SPFieldDateTime)oSPWeb.Fields.GetFieldByInternalName(fechaRecepcionField); FechaRecepcion.Group = groupColumn; FechaRecepcion.Update(); //} //Method 3 using Field schema //if (!oSPWeb.Fields.ContainsField("Nota")) //{ string notaField = oSPWeb.Fields.Add("Nota", SPFieldType.Note, false); SPFieldMultiLineText Nota = (SPFieldMultiLineText)oSPWeb.Fields.GetFieldByInternalName(notaField); Nota.Group = groupColumn; Nota.Update(); //} //if (!oSPWeb.Fields.ContainsField("Gerencias")) //{ // Lookup Column SPList gerenciasList = oSPWeb.Lists.TryGetList("Gerencias"); string GerenciaField = oSPWeb.Fields.AddLookup("Gerencia", gerenciasList.ID, true); SPFieldLookup gerenciaLookup = (SPFieldLookup)oSPWeb.Fields.GetFieldByInternalName(GerenciaField); gerenciaLookup.Group = groupColumn; gerenciaLookup.LookupField = "Title"; gerenciaLookup.Update(); //} SPList listas = oSPWeb.Lists.TryGetList("Listas"); //if (!oSPWeb.Fields.ContainsField("Nivel Prioridad")) //{ string nivelPrioridadField = oSPWeb.Fields.AddLookup("Nivel Prioridad", listas.ID, true); SPFieldLookup nivelPrioridadLookup = (SPFieldLookup)oSPWeb.Fields.GetFieldByInternalName(nivelPrioridadField); nivelPrioridadLookup.Group = groupColumn; nivelPrioridadLookup.LookupField = "Title"; nivelPrioridadLookup.Update(); //} //if (!oSPWeb.Fields.ContainsField("Clasificacion")) //{ string clasificacionField = oSPWeb.Fields.AddLookup("Clasificacion", listas.ID, true); SPFieldLookup clasificacionLookup = (SPFieldLookup)oSPWeb.Fields.GetFieldByInternalName(clasificacionField); clasificacionLookup.Group = groupColumn; clasificacionLookup.LookupField = "Title"; clasificacionLookup.Update(); //} //if (!oSPWeb.Fields.ContainsField("Estado")) //{ string estadoField = oSPWeb.Fields.AddLookup("Estado", listas.ID, false); SPFieldLookup estadoLookup = (SPFieldLookup)oSPWeb.Fields.GetFieldByInternalName(estadoField); estadoLookup.Group = groupColumn; estadoLookup.LookupField = "Title"; estadoLookup.Update(); //} } catch (Exception) { throw; } } }