public static void CreateDateTimeColumn(this SPWeb web, string group, string description, string name, string displayName, SPDateTimeFieldFormatType format = SPDateTimeFieldFormatType.DateOnly) { try { if (!web.Fields.ContainsField(name)) { string fieldName = web.Fields.Add(name, SPFieldType.DateTime, false); SPFieldDateTime field = (SPFieldDateTime)web.Fields.GetField(name); field.Group = group; field.StaticName = name; field.DisplayFormat = format; 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:CreateDateTimeColumn. MESSAGE: {0}. EXCEPTION TRACE: {1} ", ex.Message, ex.StackTrace), ex.StackTrace); } }
public static void UpdateTypeOfDateTime(string siteUrl) { if (!string.IsNullOrEmpty(siteUrl)) { List <string> colNames = new List <string>() { "StartDate", "DueDate" }; string listName = "Task List"; try { SPSecurity.RunWithElevatedPrivileges(delegate() { Console.Write("Processing..."); using (SPSite spSite = new SPSite(siteUrl)) { using (SPWeb spWeb = spSite.RootWeb) { SPList spList = spWeb.Lists.TryGetList(listName); if (spList != null) { Console.WriteLine(); Console.Write("List Name: {0}", listName); spWeb.AllowUnsafeUpdates = true; foreach (var colName in colNames) { SPField spField = spList.Fields.TryGetFieldByStaticName(colName); if (spField != null) { Console.WriteLine(); Console.Write("Column Name: {0}", colName); SPFieldDateTime spFieldDateTime = spField as SPFieldDateTime; if (!(spFieldDateTime.FriendlyDisplayFormat == SPDateTimeFieldFriendlyFormatType.Disabled)) { spFieldDateTime.FriendlyDisplayFormat = SPDateTimeFieldFriendlyFormatType.Disabled; spFieldDateTime.Update(); } Console.Write(" -> Done"); } } spWeb.AllowUnsafeUpdates = false; } } } }); } catch (Exception ex) { Console.WriteLine(string.Format("Error: {0}", ex.Message)); } Console.Read(); } else { Console.Write("Troll?"); Console.Read(); } }
private void OnListCreated(ref SPList list) { SPFieldDateTime modified = list.Fields.GetFieldByInternalName("Modified") as SPFieldDateTime; modified.FriendlyDisplayFormat = SPDateTimeFieldFriendlyFormatType.Disabled; modified.Update(); }
private void OnListCreated(ref SPList list) { SPFieldDateTime modified = list.Fields.GetFieldByInternalName("Modified") as SPFieldDateTime; modified.FriendlyDisplayFormat = SPDateTimeFieldFriendlyFormatType.Disabled; modified.Update(); list.EnableVersioning = true; list.EnableMinorVersions = true; list.MajorVersionLimit = 10; list.MajorWithMinorVersionsLimit = 3; list.DraftVersionVisibility = DraftVisibilityType.Author; list.EnableModeration = false; list.ForceCheckout = true; list.Update(); }
//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; } } }
protected void Page_Load(object sender, EventArgs e) { // Declare Fields var site = SPContext.Current.Site; var web = site.RootWeb; // Get the SPFieldCollection for the root web. var fields = web.Fields; // Add a new date field. var fieldExpiryDate = new SPFieldDateTime(fields, SPFieldType.DateTime.ToString(), "Expiry Date") { StaticName = "ExpiryDate", DisplayFormat = SPDateTimeFieldFormatType.DateOnly, Group = "Contoso Columns", Required = true }; fieldExpiryDate.Update(); fields.Add(fieldExpiryDate); // Add a new choice field. var fieldProductionType = new SPFieldChoice(fields, SPFieldType.Choice.ToString(), "Production Type") { StaticName = "ProductionType" }; fieldProductionType.Choices.Add("Research"); fieldProductionType.Choices.Add("Prototype"); fieldProductionType.Choices.Add("Trial"); fieldProductionType.Choices.Add("Release"); fieldProductionType.Group = "Contoso Columns"; fieldProductionType.Required = true; fieldProductionType.Update(); fields.Add(fieldProductionType); // Declare Content Types // Get the ID of the parent content type. var parentId = SPBuiltInContentTypeId.Document; // Create the Invoice content type. var ctInvoice = new SPContentType(parentId, web.ContentTypes, "Invoice"); // Create field links. var fldClientName = fields.GetField("ClientName"); var fldPaymentDueDate = fields.GetField("PaymentDueDate"); var fldAmount = fields.GetField("Amount"); var fldLinkClientName = new SPFieldLink(fldClientName); var fldLinkPaymentDueDate = new SPFieldLink(fldPaymentDueDate); var fldLinkAmount = new SPFieldLink(fldAmount); // Add the field links to the content type. ctInvoice.FieldLinks.Add(fldLinkClientName); ctInvoice.FieldLinks.Add(fldLinkPaymentDueDate); ctInvoice.FieldLinks.Add(fldLinkAmount); // Persist the changes to the content database. ctInvoice.Update(); // Add the content type to the collection. web.ContentTypes.Add(ctInvoice); web.Dispose(); // Bind Content Type // Get the list. var list = web.GetList("Invoices"); // Get the site content type. var ct = web.AvailableContentTypes["Invoice"]; // Enable content types on the list. list.ContentTypesEnabled = true; // Add the content type to the list. list.ContentTypes.Add(ct); // Persist the changes to the database. list.Update(); //Set Document Template ct.DocumentTemplate = "SiteAssets/MyTemplate.dotx"; ct.Update(); //Set Workflow Template SPWorkflowTemplate template = web.WorkflowTemplates.GetTemplateByName("InvoiceWorkflow", web.Locale); // Create a new workflow association. SPWorkflowAssociation association = SPWorkflowAssociation.CreateWebContentTypeAssociation(template, "Process Invoice", "Invoice Tasks", "Process Invoice History"); if (ct.WorkflowAssociations.GetAssociationByName("Process Invoice", web.Locale) == null) { ct.WorkflowAssociations.Add(association); ct.UpdateWorkflowAssociationsOnChildren(false, true, true, false); } }