Tuesday, December 29, 2015

Set value to an Managed Metadata field


Managed Metadata is an extraordinary feature in SharePoint career path. It helps organization to structure all unstructured data and it is easy to use them.

The managed metadata field can be associated with List, document library.. etc

Also it has a feature that not used values(terms) can be deprecated instead deleting them permanently from centralized location. The deprecated terms can be used for future tracking. Normally if any MMD field associated with a TermSet then user can see only Enabled terms in the TermStore tree. It is a out-of-the-box feature that SharePoint will apply filter and display only enabled terms to the end user.

But deprecated term also can be assigned to a MMD field if necessary based on business need using Server Object model.

Find the code sample below which assign value to MMD field with enabled and deprecated term.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace SetMMD_Field {

    class Program {
        static void Main(string[] args) {
            SPList lst = null; ;
            
            using (SPSite site = new SPSite(@"http://kmstechs/")) {
                using (SPWeb web = site.RootWeb) {
                  //  SPSecurity.RunWithElevatedPrivileges(delegate() {
                        lst = web.Lists["SK_Test"];
                        SPListItem oSPListItem = lst.Items.Add();
                        oSPListItem["Title"] = "Hello SharePoint";
                        TaxonomySession session = new TaxonomySession(site);

                        TaxonomyField taxfield = oSPListItem.Fields["Invalid_MMD"] as TaxonomyField;

                        Term InvalidTerm = session.GetTerm(new Guid(@"7dba48a0-89fa-4203-a265-e49ca3752ab7"));
                        string taxFieldInternalname1 = oSPListItem.Fields["Invalid_MMD"].InternalName;
                        oSPListItem[taxFieldInternalname1] = InvalidTerm.Name + "|" + InvalidTerm.Id.ToString();
                        taxfield.SetFieldValue(oSPListItem, InvalidTerm);

                        TaxonomyField taxfield2 = oSPListItem.Fields["Valid_MMD"] as TaxonomyField;

                        Term validTerm = session.GetTerm(new Guid(@"dca67b77-e4f4-4630-8785-e22518945ecc"));
                        string taxFieldInternalname2 = oSPListItem.Fields["Valid_MMD"].InternalName;
                        oSPListItem[taxFieldInternalname2] = validTerm.Name + "|" + validTerm.Id.ToString();
                        taxfield2.SetFieldValue(oSPListItem, validTerm);

                        oSPListItem.Update();

                   // });
                }
            }

            Console.WriteLine("done");

            Console.ReadKey(true);
        }
    }
}

Enjoy working with SharePoint :-)

Monday, December 21, 2015

Add more properties (metadata) to SharePoint Folder

Add more properties (metadata) to SharePoint Folder

Folder is an content type in SharePoint and it being used for categorizing OR grouping specified items/documents OR Applying Explorer view on SharePoint contents.  The folder content type provide explorer view if user wanted to navigate an document among many.

But the Folder content type is sealed in SharePoint and it will not allow the administrator to amend folder’s properties. Instead we can create a new custom content type by inheriting base folder content type and add new properties that are needed.

Below are the steps to create new folder content type with additional properties (metadata)


  1. Login to the  SharePoint site where you need folder with additional properties
  2. Navigate to Site Actions -->  Site Settings
  3. Click on Site Content Types
  4. Click on Create link
  5. Enter new content type name 
  6. Set the parent content type group as “Folder Content Types”
  7. Set the parent content type to “Folder”
  8. Provide new group name for new content type. Or else the new content type will be displayed under Custom content type group.
  9. Click OK button
  10. Click on New column and provide the column details 
  11. Add the custom content type where ever you need and update the views with new folder properties.

Note: Make sure allow custom content type option is enabled in the document library advances settings to add custom content type.