How to set O365 PageLayout Associated Content Type field

Here is a code snippet to set your page layouts associated content type. This one was a bit weird when I first tried to set it.

 private static void SetPageLayoutMetadata(Web web, File uploadFile, string title, string publishingAssociatedContentType)
{
// Speficy that the page layout is a page layout and not a master page
var parentContentTypeId = ""0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE811""; //Page Layout
var gallery = web.GetCatalog(116);
web.Context.Load(gallery, g => g.ContentTypes);
web.Context.ExecuteQuery();

var contentTypeId = gallery.ContentTypes.FirstOrDefault(ct => ct.StringId.StartsWith(parentContentTypeId)).StringId;
var item = uploadFile.ListItemAllFields;
web.Context.Load(item);

item["ContentTypeId"] = contentTypeId;
item["Title"] = title;
item["PublishingAssociatedContentType"] = publishingAssociatedContentType;

item.Update();
web.Context.ExecuteQuery();
} 

Two important things:

  • parentContentTypeId: This is the ID that tells O365 that this page layout is actually page layout, this is a static id which is defined in the code
  • publishingAssociatedContentType: This is your actual content type to which you want to associate your page layout to: Example of the format:;#my content type name;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00A3428808D7DE6A4RABF83921AFB25424;#
    • So the format should be in the following way:

public static String BuildPublishingAssociativeContentTypeId(ContentType contentType)
{
String contentTypeAssociativeID = null;
if(contentType != null)
{
contentTypeAssociativeID = String.Format(";#{0};#{1};#", contentType.Name, contentType.StringId);
}

return contentTypeAssociativeID;
}

Advertisements

SharePoint solution package installation fails when activating feature that updates fields

If you happen to get the following errors related to site columns, features activation and/or solution package installation:

Unable to locate the xml-definition for FieldName with FieldId ‘Your field GUID’, exception: Microsoft.SharePoint.SPException —> System.Runtime.InteropServices.COMException: <nativehr>0x8000ffff</nativehr><nativestack></nativestack>
at Microsoft.SharePoint.Library.SPRequestInternalClass.GetGlobalContentTypeXml(String bstrUrl, Int32 type, UInt32 lcid, Object varIdBytes)
at Microsoft.SharePoint.Library.SPRequest.GetGlobalContentTypeXml(String bstrUrl, Int32 type, UInt32 lcid, Object varIdBytes)

Or Error message:

Field not found or invalid – ID: [your field GUID]

Then you have two options:

  1. Set the field attributes ‘Overwrite‘ and/or ‘OverwriteInChildScopes‘ to True => http://msdn.microsoft.com/en-us/library/office/aa979575(v=office.15).aspx
  2. Or if this does not help then what will kill your Microsoft support for you SharePoint installation is to modify the database directly(not recommended BUT if it is not a production environment and for some reason you need to do something quick and dirty to test something then this might be an option) =>http://shipoint.com/2012/06/07/removing-a-corrupted-site-column-in-sharepoint-2010/

 

Update Site Default Page(Welcome Page) Content Type with PowerShell

This is a script that you can use to update a content type of your default page. With a little modification you can use bits of this script to update any page or list item content type.

param(
[string]$url
)

Start-Transcript -Path “output.log” -Append -Force -Confirm:$false

if (-not $url)
{
Write-Host “Specify site url in url parameter” -foregroundcolor red
return
}
Write-Host “##################################################” -foregroundcolor White
Install-SPFeature -Path “feature name”
Write-Host “Feature was sucessfully installed” -foregroundcolor green
Enable-SPFeature -Identity “feature name” -Url $url
Write-Host “Feature was sucessfully activated on” $url “site” -foregroundcolor green

Write-Host “##################################################” -foregroundcolor White

Write-Host “Opening Root Site” $url -foregroundcolor green

$web = Get-SPWeb $url
Write-Host “Retrieving default Page” -foregroundcolor yellow
$welcomePage = $web.GetListItem($web.RootFolder.WelcomePage)
Write-Host “Assigning the new content type to the default Page” -foregroundcolor yellow
$welcomePage[“ContentTypeId”] = $welcomePage.ParentList.ContentTypes[“new content type name”].Id
Write-Host “Updating the default Page” -foregroundcolor yellow
$welcomePage.SystemUpdate()
Write-Host “Update finished to the default Page” -foregroundcolor green
Write-Host “##################################################” -foregroundcolor White
Stop-Transcript

Hiding Custom SharePoint fields from edit mode, new window or display mode

A quick post:

Lets say that you do not want to allow users to possibility to edit and insert values to a custom field in one of your lists. To do this you can set the values of one of the following properties of a SPField object:

  1. ShowInEditForm
  2. ShowInDisplayForm
  3. ShowInNewForm

You can do this programmatically at any time, below is a link on how to do it:

http://www.sharepointdynamics.net/2011/09/how-to-hide-a-sharepoint-list-column-from-a-list-form-new-edit-and-display/


#Get the web and site column objects
$web = Get-SPWeb http://portal
$column = $web.Fields["Test Column"]

#Set the PushChangesToLists property for the changes to be applied
#to lists where the column has already been added
$column.PushChangesToLists = $true

#Change the ShowInEditForm and other properties and update objects
$column.ShowInEditForm = $false
$column.ShowInNewForm = $false
$column.ShowInDisplayForm = $true
$column.ShowInViewForms = $true

$column.Update()
$web.Update()
$web.Dispose()

Remember to check that the change is applied to the list field also. If not then do the change directly to the list field.

 

Or by XML definition:

<Field Type=”Number” ShowInEditForm=”FALSE” ShowInNewForm=”FALSE” DisplayName=”Counter” Required=”FALSE” EnforceUniqueValues=”FALSE” Indexed=”FALSE” Group=”My Group” ID=”{7f111eff-569d-4852 8bf2-ba0fcee6c69a}” SourceID=”{b1498d8e-4ad9-4d0d-8584-53d9a5de8c11}” StaticName=”Counter” Name=”Counter”>
<Default>0</Default>
</Field>

Links to MSDN about the properties:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showineditform.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showindisplayform.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showinnewform.aspx