FTC to CAM – Custom actions and property bag entries from SP App

As part of my work with few global customers related on the transition from full trust code (FTC) to cloud application model (CAM), I have had numerous discussion related on the capabilities and patterns which are not available in the CAM side. One of the classic discussion or challenge is the pattern where some of the web parts or other behaviour in site is dependent on settings in the property bag of the site.

Classically these kind of scenarios have been solved by deploying a application page to the web application, which is accessed either from site settings or from site actions. These links have been usually added using CustomAction element in feature framework. This pattern does not however work unless you are deploying full trust code (FTC) and that would limit the reach of the customization.

Objective of this blog post is to explain how to achieve same end result, using the CAM or SP App approach.

Natively we don’t support adding site action and site setting links from SP App directly, but you can however use CSOM to achieve this. This is pretty simple and does only require few lines of code achieve.


 public void AddCustomActions(ClientContext clientContext)
{
    // Add site settings link, if it doesn't already exist
    if (!CustomActionAlreadyExists(clientContext, "Sample_CustomSiteSetting"))
    {
        // Add site settings link
        UserCustomAction siteSettingLink = clientContext.Web.UserCustomActions.Add();
        siteSettingLink.Group = "SiteTasks";
        siteSettingLink.Location = "Microsoft.SharePoint.SiteSettings";
        siteSettingLink.Name = "Sample_CustomSiteSetting";
        siteSettingLink.Sequence = 1000;
        siteSettingLink.Url = string.Format(DeployManager.appUrl, clientContext.Url);
        siteSettingLink.Title = "Modify Site Metadata";
        siteSettingLink.Update();
        clientContext.ExecuteQuery();
    }

    // Add site actions link, if it doesn't already exist
    if (!CustomActionAlreadyExists(clientContext, "Sample_CustomAction"))
    {
        UserCustomAction siteAction = clientContext.Web.UserCustomActions.Add();
        siteAction.Group = "SiteActions";
        siteAction.Location = "Microsoft.SharePoint.StandardMenu";
        siteAction.Name = "Sample_CustomAction";
        siteAction.Sequence = 1000;
        siteAction.Url = string.Format(DeployManager.appUrl, clientContext.Url); ;
        siteAction.Title = "Modify Site Metadata";
        siteAction.Update();
        clientContext.ExecuteQuery();
    }
}

You can actually add these links to multiple other locations as well using the same pattern, since all the same location identifiers are available, like for the CustomAction feature framework element. After above lines have been executed, you will have following changes in the site which the code was executed against.

image

image

When these links are clicked, user is being redirected to the provider hosted app side for enabling the modification of the site metadata.

image

In this demonstration we save this site metadata directly to the property bag of the host web, but we really only utilize it for presentational purposes. In real life scenarios this kind of metadata can drive multiple difference scenarios from archiving to different functionalities what we shown on the site.

Video of the demo

Following video walks through the code and explains how example was implemented. Notice that used code in the demo is also available for download from following link

Video is available for download from my SkyDrive if needed for offline purposes.