5

I need to create a content type from my schema file which containe all my fields this is how to create a basic content type:

function  Add-metaXmlFields-ToContentType($ctx, $xmlFilePath, $CT_id)
{   
    $web = $ctx.Web
    $site = $ctx.Site
    $ctx.Load($web)
    $ctx.Load($site)

        try
        {
            $ctx.executeQuery()
            write-host "info: Loaded Fields and Content Types" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }


        $ContentType = $ctx.web.contenttypes.getbyid($CT_id)


       [xml] $xmldata = [xml](Get-Content($xmlFilePath));
            if (-not $xmldata)
            {
                return
            }   
        Write-Host "Solutions xml file loaded successfully"



            foreach ($node in $xmldata.Elements.ChildNodes) {

                $field=$web.Fields.AddFieldAsXml($node.OuterXml,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint); 
                # Finally, add the custom site column to our custom content typ$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation

                $fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
                $fieldReferenceLink.Field = $field;
                $ContentType.FieldLinks.Add($fieldReferenceLink)
            }

            # Commit changes to SharePoint
            $ContentType.Update($true)
            $ctx.ExecuteQuery()

        }

the probleme here is :

$node.OuterXml= 
<Field ID="{45FC08E4-15ED-4B99-BD0C-EAFC4C371D7A}" Name="Service Title" DisplayName="Service Title" Type="TaxonomyFieldType" Required="FALSE" Group="Custom Site Columns" xmlns="http://schema
s.microsoft.com/sharepoint/"></Field>

but :

$fieldReferenceLink.Field

AutoIndexed                 : 
CanBeDeleted                : 
DefaultValue                : 
Description                 : 
DescriptionResource         : Microsoft.SharePoint.Client.UserResource
Direction                   : 
EnforceUniqueValues         : 
EntityPropertyName          : 
Filterable                  : 
FromBaseType                : 
Group                       : 
Hidden                      : 
Id                          : 
Indexed                     : 
InternalName                : 
JSLink                      : 
ReadOnlyField               : 
Required                    : 
SchemaXml                   : 
SchemaXmlWithResourceTokens : 
Scope                       : 
Sealed                      : 
Sortable                    : 
StaticName                  : 
Title                       : 
TitleResource               : Microsoft.SharePoint.Client.UserResource
FieldTypeKind               : 
TypeAsString                : 
TypeDisplayName             : 
TypeShortDescription        : 
ValidationFormula           : 
ValidationMessage           : 
Context                     : Microsoft.SharePoint.Client.ClientContext
Tag                         : 
Path                        : Microsoft.SharePoint.Client.ObjectPathMethod
ObjectVersion               : 
ServerObjectIsNull          : 
TypedObject                 : Microsoft.SharePoint.Client.Field

it like no mapping there . any idea .

  • 1
    Is there a problem with the code? – wjervis Dec 21 '16 at 18:46
  • @wijervis i update the code – Imen Turki Dec 22 '16 at 12:22
5

After a lot of investigation, it works now , only need : $context.Load() after every get context element ! this is the new version of the code :

$web = $ctx.Web
    $site = $ctx.Site
    $ctx.Load($web)
    $ctx.Load($site)
    $Fields=$ctx.web.Fields
    $ctx.Load($Fields)
        try
        {
            $ctx.executeQuery()
            write-host "info: Loaded Fields and Content Types" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }


        $ContentType = $ctx.web.contenttypes.getbyid($CT_id)


       [xml] $xmldata = [xml](Get-Content($xmlFilePath));
            if (-not $xmldata)
            {
                return
            }   
        Write-Host "Solutions xml file loaded successfully"

             $ctx.Load($ContentType)
             $ctx.ExecuteQuery()

            foreach ($node in $xmldata.Elements.ChildNodes) {

                $field=$Fields.AddFieldAsXml($node.OuterXml,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint);  
                # Finally, add the custom site column to our custom content typ$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation

                $fieldReferenceLink = new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
                $fieldReferenceLink.Field = $field
            $addContentType = $ContentType.FieldLinks.Add($fieldReferenceLink)
            write-host "info: added $($node.OuterXml) to array" -foregroundcolor green
        } 

         $ContentType.Update($true)         
        # send the request containing all operations to the server
        try{
            $ctx.executeQuery()
            write-host "info: Added columns to content type" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }

And this is my Fiel.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  <Field
       ID="{45FC08E4-15ED-4B99-BD0C-EAFC4C371D7A}"
       Name="a"
       DisplayName="a"
       Type="TaxonomyFieldType"
       Required="FALSE"
       Group="Custom Site Columns">
  </Field>

  <Field
      ID="{C574403E-6A2B-42D7-958E-017EF21D210E}"
      Name="b"
      DisplayName="b"
      Type="Note"
      Required="FALSE"
      Group="Custom Site Columns">
  </Field>
</Elements>

Hope it help.

  • hope it help @MonkeyWrench – Imen Turki Dec 22 '16 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.