Monday, September 8, 2014

SharePoint 2013 - Shared Column / fields and allowed contenttypes setup - powershell

Powershell:

#Get input XML

$xmlFile1  =  "$dp0\SharedColumns.xml"
                $col = [xml](Get-Content $xmlFile1)
                $sharedcol=$col.SiteColumns.Columns.Field.Name
                 $FieldName = $_.Name        
                    $myCT=$web.ContentTypes[$contentTypeName]

#Set Shared Columns
                    $sharedFileXMLPrefix=”"
                    $sharedFileXML=""
                    $sharedFileXMLSuffix="

                 
                       $sharedcol | ForEach {
                       Write-Host $_
                       $id=$myCT.Fields[$_].Id


                        $sharedFileXML=$sharedFileXML+""
                            Write-Host $sharedFileXML
                         
                        }

                $FinalValue=$sharedFileXMLPrefix+$sharedFileXML+ $sharedFileXMLSuffix
                Write-Host $FinalValue
                $oXMLDocument=New-Object System.XML.XMLDocument
                $ns = New-Object Xml.XmlNamespaceManager $oXMLDocument.NameTable
                $ns.AddNamespace( "NamespaceURI", "http://schemas.microsoft.com/office/documentsets/sharedfields" )
                $oXMLDocument.LoadXML($FinalValue);
                Write-Host $oXMLDocument.ToString();
                $myCT.XmlDocuments.Delete("http://schemas.microsoft.com/office/documentsets/sharedfields");
                $myCT.XmlDocuments.Add($oXMLDocument);


#Set Allowed ContentType

                $AllowedCTName=$col.SiteColumns.Columns.AllowedCT
                    Write-Host $AllowedCTName
                    $AllowedCT=$web.ContentTypes[$AllowedCTName]
                    $AllowedCTXMLPrefix=”"
                    $AllowedCTXML=""
                    $AllowedCTXMLSuffix="
”               
                       $AllowedCTid=$AllowedCT.Id
                        $AllowedCTXML=$AllowedCTXML+""
                            Write-Host $AllowedCTXML
                         



                $AllowedCTFinalValue=$AllowedCTXMLPrefix+$AllowedCTXML+ $AllowedCTXMLSuffix
                Write-Host $AllowedCTFinalValue
                $AllowedCTXML=New-Object System.XML.XMLDocument
                $ns1 = New-Object Xml.XmlNamespaceManager $AllowedCTXML.NameTable
                $ns1.AddNamespace( "NamespaceURI", "http://schemas.microsoft.com/office/documentsets/allowedcontenttypes" )
                $AllowedCTXML.LoadXML($AllowedCTFinalValue);
                Write-Host $AllowedCTFinalValue.ToString();
                $myCT.XmlDocuments.Delete("http://schemas.microsoft.com/office/documentsets/allowedcontenttypes");
                $myCT.XmlDocuments.Add($AllowedCTFinalValue);




                $myCT.Update();


XML:



 
   
 

2 comments: