4

Need to add SEO properties to a SharePoint 2013 site programmatically. I know how to accomplish this in the UI but not through code. Any help would be great.

Thanks!!!!

5

For the robots.txt exclusion you'd use the following sort of code. Imagine you also had a site called topsecretyou wanted to exclude.

StringBuilder robots = new StringBuilder();
robots.AppendLine("User-agent: *");
robots.AppendLine("Disallow: /_layouts/");
robots.AppendLine("Disallow: /_vti_bin/");
robots.AppendLine("Disallow: /_catalogs/");
robots.AppendLine("Disallow: /toppsecret/");
web.SetProperty("xmlsitemaprobotstxtpropertyname", robots.ToString());

For the Bing specific meta tag for web verification...

string newCustomMeta = "<meta name=\"msvalidate.01\" content=\"0123456789ABCDEF0123456789ABCDEF\" />";
web.SetProperty("seoincludecustommetatagpropertyname", true.ToString());
web.SetProperty("seocustommetatagpropertyname", newCustomMeta);

For the canonial URL tag...

web.SetProperty("seoenablecanonicallinkparameterspropertyname", true.ToString());
web.SetProperty("seocanonicallinkparameterlistpropertyname", "category");
web.Update();

For MMS-based navigation SEO

TaxonomySession taxSession = new TaxonomySession(site, updateCache: true);
TermStore termStore = taxSession.DefaultSiteCollectionTermStore;
Group termGroup = termStore.GetSiteCollectionGroup(site, true);

// locate the navigation term set for the site collection (there can be only one)
foreach (TermSet termSet in termGroup.TermSets)
{
    NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(termSet, site.RootWeb, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);
    if (navTermSet.IsNavigationTermSet)
    {
        // determine which navigation nodes are associated with Welcome Page content types
        foreach (NavigationTerm navTerm in navTermSet.Terms)
        {
            string pageUrl = SPUtility.GetServerRelativeUrlFromPrefixedUrl(navTerm.TargetUrl.Value);
            SPListItem pageItem = web.GetListItem(pageUrl);
            if (pageItem.ContentType.Name == "Welcome Page")
            {
                // set the SEO properties on the Navigation Term (all will have same SEO tags)
                Term term = termSet.GetTerm(navTerm.Id);
                term.SetLocalCustomProperty("_Sys_Seo_PropBrowserTitle", Properties.Resources.SeoBrowserTitle);
                term.SetLocalCustomProperty("_Sys_Seo_PropDescription", Properties.Resources.SeoDescription);
                term.SetLocalCustomProperty("_Sys_Seo_PropKeywords", Properties.Resources.SeoKeywords);
                term.SetLocalCustomProperty("_Sys_Seo_PropSiteNoIndex", false.ToString());
            }
        }

        break;
    }
}

termStore.CommitAll();
web.Update();
  • Rob, will this MMS Navigation SEO also work if I'm using Structural Navigation? – nholloway4 Mar 20 '14 at 3:58

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.