{"id":1041,"date":"2014-09-30T07:22:56","date_gmt":"2014-09-30T07:22:56","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1041"},"modified":"2024-04-11T12:33:45","modified_gmt":"2024-04-11T12:33:45","slug":"creating-fields-and-content-types-cleanly-in-sharepoint","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/creating-fields-and-content-types-cleanly-in-sharepoint\/","title":{"rendered":"Creating Fields and Content Types cleanly in SharePoint"},"content":{"rendered":"<p>when creating Content Types in SharePoint, it\u2019s nice to script the process, for planning and repeatability. Here\u2019s a nifty routine that allows a single line creation of Site Columns and Content Types.<\/p>\n<p>First, here\u2019s the routine to create Site Columns. The here-string XML is replaced with placeholders filled by parameters. Note the convenient parameters:<\/p>\n<pre lang=\"php\">function tryAdd-TextField ([Microsoft.SharePoint.SPWeb] $web, [string] $DisplayName, [string] $InternalName, [string] $Group, [Boolean] $Hidden, \r\n[Boolean] $Required, [Boolean] $ShowInDisplayForm, [Boolean] $ShowInEditForm, [Boolean] $ShowInNewForm )\r\n{\r\n$fields = $web.fields;\r\ntry\r\n{\r\n    $q=$fields.getFieldByInternalName($InternalName); \r\n} catch\r\n{\r\n    $q=$null;\r\n}\r\n \r\nif ($q -ne $null)\r\n{\r\nWrite-Host \"$($InternalName) already exists!, no action taken to create this site column\"\r\n}\r\nelse\r\n{\r\n$fieldXMLString = '\r\n '\r\n \r\n $FieldXMLString = $FieldXMLString.Replace(\"@InternalName\",$InternalName.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@DisplayName\",$DisplayName.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@Group\",$Group.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@Hidden\",$Hidden.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@Required\",$Required.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@ShowInDisplayForm\",$ShowInDisplayForm.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@ShowInEditForm\",$ShowInEditForm.tostring())\r\n $FieldXMLString = $FieldXMLString.Replace(\"@ShowInNewForm\",$ShowInNewForm.tostring())\r\n \r\n $web.Fields.AddFieldAsXml($fieldXMLString)\r\n }\r\n }\r\n<\/pre>\n<pre lang=\"php\">function tryAdd-CT ([Microsoft.SharePoint.SPWeb] $web, [string] $Field, [string] $Group, [string] $Parent, [string] $Name, [string] $Description )\r\n{\r\n    try\r\n    {\r\n        $ctypeParent = $web.availablecontenttypes[$Parent]\r\n \r\n    }\r\n    catch\r\n    {\r\n        $ctypeParent = $null;\r\n    }\r\n \r\n    if ($ctypeParent -eq $null)\r\n    {\r\n        write-host \"Content Type $($Name) not created because there was a problem finding the Parent Content Type $($Parent)\"\r\n    }\r\n    else\r\n    {\r\n    $ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $Name)\r\n    $ctype.Description = $Description\r\n    $ctype.group = $Group\r\n \r\n    if (![string]::IsNullOrEmpty($field))\r\n    {\r\n    foreach ($fld in $field.split(\"|\"))\r\n     {\r\n        $f=$web.fields.getFieldByInternalName($fld)  \r\n        $link = new-object Microsoft.SharePoint.SPFieldLink $f\r\n        $ctype.FieldLinks.Add($link)\r\n     }\r\n    }\r\n \r\n    try\r\n    {\r\n        $ctype = $web.contenttypes.add($ctype)\r\n    }\r\n    catch\r\n    {\r\n        write-host \"Content Type $($Name) already exists\"\r\n    }\r\n    }\r\n}<\/pre>\n<p>Let\u2019s now create some site columns:<\/p>\n<pre lang=\"php\">tryAdd-TextField -web $Web -DisplayName \"Year\"    -InternalName \"YearACT\"    -Group \"Actuarial\" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False\r\ntryAdd-TextField -web $Web -DisplayName \"Quarter\" -InternalName \"QuarterACT\" -Group \"Actuarial\" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False\r\ntryAdd-TextField -web $Web -DisplayName \"Month\"   -InternalName \"MonthACT\"   -Group \"Actuarial\" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False\r\ntryAdd-TextField -web $Web -DisplayName \"LOB\"     -InternalName \"LOBACT\"     -Group \"Actuarial\" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False\r\n<\/pre>\n<p>Here\u2019s how to create a Content Type with one field:<\/p>\n<pre lang=\"php\">\r\ntryAdd-CT -web $Web -Field $null -Group \"Actuarial\"  -Parent (select-parent)  -Name \"My Core ACT\" -Description \"Core Actuarial Document\"\r\n<\/pre>\n<p>Note in the comment below, the fields can be passed in pipe-delimited<\/p>\n<pre lang=\"php\">\r\ntryAdd-CT -web $Web -Field \"YearACT|QuarterACT|MonthACT|LOBACT\" -Group \"Actuarial\"  -Parent \"My Core ACT\" -Name \"General (ACT)\" -Description \"General Actuarial Document\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>when creating Content Types in SharePoint, it\u2019s nice to script the process, for planning and repeatability. Here\u2019s a nifty routine that allows a single line creation of Site Columns and Content Types. First, here\u2019s the routine to create Site Columns. The here-string XML is replaced with placeholders filled by parameters. Note the convenient parameters: function [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1044,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-content-types"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":11,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":1061,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1041\/revisions\/1061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1044"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}