/// <summary> /// Checks if a site column exists in a site and creates it if it doesn't exist. /// </summary> /// <param name="site">The site to check.</param> /// <param name="columnDetail">The details of the site column.</param> /// <param name="isNewColumn">A boolean that is set to true, if a new column is created; otherwise, it is set to false.</param> /// <returns>A SPField object that references an existing or newly created site column.</returns> public static SPField EnsureSiteColumn(this SPWeb site, ColumnDetails columnDetail, ref bool isNewColumn) { isNewColumn = false; SPField column = null; if (!site.Fields.ContainsField(columnDetail.InternalName)) { site.Fields.AddFieldAsXml(columnDetail.GetXml()); column = site.Fields.GetFieldByInternalName(columnDetail.InternalName); isNewColumn = true; } else { column = site.Fields.GetFieldByInternalName(columnDetail.InternalName); } return column; }
/// <summary> /// Checks if a column exists in a list and creates it if it doesn't exist. /// </summary> /// <param name="site">The site to check.</param> /// <param name="columnDetail">The details of the site column.</param> /// <param name="isNewColumn">A boolean that is set to true, if a new column is created; otherwise, it is set to false.</param> /// <returns>A SPField object that references an existing or newly created site column.</returns> public static SPField EnsureColumn(this SPList list, ColumnDetails columnDetail, bool isSiteColumn, ref bool isNewColumn) { isNewColumn = false; SPField column = null; if (!list.Fields.ContainsField(columnDetail.InternalName)) { if (!isSiteColumn) { // When a column is created using AddAsXml in a list (using it in a site isn't affected), set the display name to the internal name to workaround a bug that creates the column using the display name. ColumnDetails tempDetail = new ColumnDetails(columnDetail); if (tempDetail.Id.Equals(Guid.Empty)) { tempDetail.Id = Guid.NewGuid(); } tempDetail.DisplayName = tempDetail.InternalName; string internalName = list.Fields.AddFieldAsXml(tempDetail.GetXml()); column = list.Fields.GetFieldByInternalName(internalName); column.Title = columnDetail.DisplayName; column.Update(); column = list.Fields.GetFieldByInternalName(internalName); //refresh the context } else { SPField siteColumn = list.ParentWeb.AvailableFields.GetFieldByInternalName(columnDetail.InternalName); string internalName = list.Fields.Add(siteColumn); column = list.Fields.GetFieldByInternalName(internalName); } isNewColumn = true; } else { column = list.Fields.GetFieldByInternalName(columnDetail.InternalName); } return(column); }
/// <summary> /// Checks if a column exists in a list and creates it if it doesn't exist. /// </summary> /// <param name="site">The site to check.</param> /// <param name="columnDetail">The details of the site column.</param> /// <param name="isNewColumn">A boolean that is set to true, if a new column is created; otherwise, it is set to false.</param> /// <returns>A SPField object that references an existing or newly created site column.</returns> public static SPField EnsureColumn(this SPList list, ColumnDetails columnDetail, bool isSiteColumn, ref bool isNewColumn) { isNewColumn = false; SPField column = null; if (!list.Fields.ContainsField(columnDetail.InternalName)) { if (!isSiteColumn) { // When a column is created using AddAsXml in a list (using it in a site isn't affected), set the display name to the internal name to workaround a bug that creates the column using the display name. ColumnDetails tempDetail = new ColumnDetails(columnDetail); if (tempDetail.Id.Equals(Guid.Empty)) { tempDetail.Id = Guid.NewGuid(); } tempDetail.DisplayName = tempDetail.InternalName; string internalName = list.Fields.AddFieldAsXml(tempDetail.GetXml()); column = list.Fields.GetFieldByInternalName(internalName); column.Title = columnDetail.DisplayName; column.Update(); column = list.Fields.GetFieldByInternalName(internalName); //refresh the context } else { SPField siteColumn = list.ParentWeb.AvailableFields.GetFieldByInternalName(columnDetail.InternalName); string internalName = list.Fields.Add(siteColumn); column = list.Fields.GetFieldByInternalName(internalName); } isNewColumn = true; } else { column = list.Fields.GetFieldByInternalName(columnDetail.InternalName); } return column; }