Follow Me

How to use ‘Microsoft.SharePoint.Publishing’ assembly in a Sandboxed Solution

By Stefan Feenstra   /     Aug 01, 2013  /     SharePoint 2013  /     , , ,

When you try to use the ‘Microsoft.SharePoint.Publishing.dll’ in your sandboxed solution you will get the following error:

You could still use the benefits of ‘Microsoft.SharePoint.Publishing.dll’. First download the tool ILSpy (ILSpy is a free .NET Assembly Browser and decompiler). With this tool it’s possible to view the content of this dll file.

For example you would like to configure the associated content type of a page layout programmatically :

Normal solution with Microsoft.SharePoint.Publishing

var exampleContentType = new SPContentType(…
var examplePageLayout = new PageLayout(…
examplePageLayout.AssociatedContentType = exampleContentType;

Solution without Microsoft.SharePoint.Publishing

  1. Load the ‘Microsoft.SharePoint.Publishing.dll’ in ILSpy
  2. Search for the ‘PageLayout’ class
  3. Search for the property ‘AssociatedContentType’
  4. This property contains the following code:

  5. Now when you click on the FieldId.AssociatedContentType reference you will see this is a Guid that references to a field in SPListItem

  6. Conclusion: you could also write:

    var examplePageLayoutListItem = new SPListItem(…
    var AssociatedContentType = new Guid(“b510aac1-bba3-4652-ab70-2d756c29540f”);
    examplePageLayoutListItem[AssociatedContentType] = exampleContentType;
    examplePageLayoutListitem.Update();

Now you don’t need to use the Microsoft.SharePoint.Publishing dll in your solution. (Also working on SharePoint Online)
This was just a very small example of what you can do with the Microsoft.SharePoint.Publishing dll in a Sandboxed Solution.

2 Comments

  1. Harsh Bhavsar Says: 23-09-2014 12:39

    Hy Stefan Feenstra,
    can we use all the functionalities of publishing dll in sandbox solution from this?

    Reply this comment
    • Stefan Feenstra Says: 23-09-2014 13:08

      Hello Harsh Bhavsar,

      I guess you can’t use all the functionalities of the publishing dll. But you could try it out and see what is inside this publishing dll and use this in your own code.

      Reply this comment

Post a Comment

Your email address will not be published. Required fields are marked *

*