Feature Receivers

Provisionate MasterPage and CSS with Feature

Posted on Updated on

Sometimes, when you don’t want to use Powershell, you can set your branding from a Feature, it’s the old procedure but very “elastic”.

In the bellow sample, I’ll give you the option to change you master page to Front Office and Back Office, you can also define your CSS File.

I’m processing all Sub Webs also, just change what you want. This is a Feature SCOPE Site

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (var site = properties.Feature.Parent as SPSite)
    {
        if (site == null) return;
        using (var web = site.OpenWeb())
        { 
            var masterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/YOURCustomFO.master" : web.Site.ServerRelativeUrl + 
                                                            "/_catalogs/masterpage/YOURCustomFO.master";
            var customMasterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/seattle.master" : web.Site.ServerRelativeUrl + 
                                                         "/_catalogs/masterpage/seattle.master";
            //string cssUrl = web.Site.ServerRelativeUrl == "/" ? "/Style Library/xx.css" : web.Site.ServerRelativeUrl + "/Style Library/xx.css";

            #region MasterPage e CSS

            // Define Master page for FO and BO
            web.CustomMasterUrl = masterUrl;
            web.MasterUrl = customMasterUrl;

            //Alternate CSS URL 
            //web.AlternateCssUrl = cssUrl;

            // inheritance settings
            web.AllProperties["__InheritsMasterUrl"] = "True";
            web.AllProperties["__InheritsCustomMasterUrl"] = "True";
            //web.AllProperties["__InheritsAlternateCssUrl"] = "True";

            web.Update();

            foreach (SPWeb subWeb in web.Webs)
            {
                try
                {
                    ProcessSubWebs(subWeb);
                }
                finally 
                {
                    if (subWeb != null)
                        subWeb.Dispose();
                }
            }
            #endregion
        }
    }
} 

Create Terms in Metadata Service Programmatically

Posted on Updated on

Working with Taxonomy Fields that created in Visual Studio without OOTB, it’s not a peace of cake. You have to follow some rules:

  1. In a Feature Receiver, Scoped SITE use the bellow Override
  2. Then you have to activate your field  (Bind) to a specific GUID, if you not it become disabled “Grayed”
    1. Use class bellow “Taxonomy”
  3. For XML Declarative find here you may find other options also
using UTIL = ChangeYourNameSpace.Utils; 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            var site = properties.Feature.Parent as SPSite;
            var session = new TaxonomySession(site);

            if (session.TermStores.Count == 0) return;
            var termStore = session.TermStores["Managed Metadata Service"];

            if (termStore.Groups.All(x => x.Name != "MY TAGs"))
            {
                //TAGS
                var group = termStore.CreateGroup("MY TAGA");
                var termSetTag = group.CreateTermSet("TAGs");
                termSetTag.CreateTerm("Tag 1", 1033);
                termSetTag.CreateTerm("Tag 2", 1033);
                termSetTag.CreateTerm("Tag 3", 1033);                
                termSetTag.IsAvailableForTagging = true;
                termSetTag.IsOpenForTermCreation = true;
                termSetTag.Contact = "intranet@yourcorp.pt";
            }

            if (termStore.Groups.All(x => x.Name != "MY Documentos"))
            {
                //Tech Documents
                var groupDoc = termStore.CreateGroup("MY Documentos");

                var termSetDocTec = groupDoc.CreateTermSet("My Docs 1");
                termSetDocTec.CreateTerm("Tag 1", 1033);
                termSetDocTec.CreateTerm("Tag 2", 1033);
                termSetDocTec.CreateTerm("Tag 3", 1033);
                termSetDocTec.IsAvailableForTagging = true;
                termSetDocTec.IsOpenForTermCreation = true;
                termSetDocTec.Contact = "intranet@yourcorp.pt";

                var termSetDocCorp = groupDoc.CreateTermSet("My Docks 2");
                termSetDocCorp.CreateTerm("Tag 1", 1033);
                termSetDocCorp.CreateTerm("Tag 2", 1033);
                termSetDocCorp.CreateTerm("Tag 3", 1033);               
                termSetDocCorp.IsAvailableForTagging = true;
                termSetDocCorp.IsOpenForTermCreation = true;
                termSetDocCorp.Contact = "intranet@yourcorp.pt";
            }

            termStore.CommitAll();

            //TAGGING
            UTIL.Taxonomy.ActivateTaxField(site, "{9358ED54-0957-4A56-8047-F940C60D2024}", "TAG's", "TAGs");

            //Documents
            UTIL.Taxonomy.ActivateTaxField(site, "{ECA6E20C-20E0-433C-8D3C-667AD9C25E99}", "Documents", "My Documents");
        });
    }
    catch (Exception ex)
    {
        //Log
    }
}

public static class Taxonomy
{
    public static void ActivateTaxField(SPSite site, string taxonomyGuid,
        string taxonomyGroup, string taxonomyTerm)
    {
        try
        {
            var fieldId = new Guid(taxonomyGuid);
            if (!site.RootWeb.Fields.Contains(fieldId)) return;

            var session = new TaxonomySession(site);

            if (session.TermStores.Count == 0) return;
            var termStore = session.TermStores["Managed Metadata Service"];
            var group = termStore.Groups[taxonomyGroup];
            var termSet = @group.TermSets[taxonomyTerm];

            var field = site.RootWeb.Fields[fieldId] as TaxonomyField;

            if (field == null) return;
            field.SspId = termSet.TermStore.Id;
            field.TermSetId = termSet.Id;
            field.TargetTemplate = string.Empty;
            field.AnchorId = Guid.Empty;
            field.Update();
        }
        catch (Exception ex)
        {
            //Log
        }

    }
} 

Good Luck 😉

Add Enterprise Keywords to List with Receiver

Posted on Updated on

Let’s see, a easy tag field is the “TaxKeyword”, the default for “Enterprise Keywords”. The advantage is that he is already at the Search Schema an Indexed, so you can use in all objects that you want, Page Layouts for Publishing Pages, Lists, whatever.

How to do it in a SharePoint Feature Receiver?

SCOPE: Web

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var web = properties.Feature.Parent as SPWeb;
    AddDocs(web);
}

private void AddDocs(SPWeb web)
{
    if (web == null) return;
    var list = web.Lists.TryGetList("Documents");
    if (list != null)
    {
        var field = list.ParentWeb.AvailableFields["Enterprise Keywords"];
        if (!list.Fields.ContainsField("Enterprise Keywords"))
        {
            list.Fields.Add(field);
            list.EnableModeration = true;
            list.EnableVersioning = true;

            var view = list.Views[0];
            view.ViewFields.Add(field);
            view.Update();

            list.Update();
            web.Dispose();
        }
        else
        {
            //Log
        }
    }
    else
    {
        //Log
    }
} 

 

How to add it to a Page Layout?

<%@ Register TagPrefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


 <Taxonomy:TaxonomyFieldControl FieldName="TaxKeyword" InputFieldLabel="My Custom Tags" runat="server"></Taxonomy:TaxonomyFieldControl>