Saturday, April 20, 2013

Adding term in TermSet SharePoint

This code will be useful for understanding on how to add the "Term" into TermSet through programmatically. First create your new Metadata service and associate them in to site collection. This picture will explain the hierarchy of the taxonomy in SharePoint 2010 and SharePoint 2013.
I am trying to add the new term to termset called "Food"

                TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
                TermStore store = session.TermStores[0];
                Group group = store.Groups[0];
                TermSet set = group.TermSets[1];
                set.CreateTerm("Vada Payasam", 1033);
                store.CommitAll();
            

Friday, April 19, 2013

SPFieldUrl adding to contenttype

This code snippet may be simple but I spent couple of hours to get things done.On my searching I learned the difference between SPFieldUrlValue,SPFieldType.URL and SPFieldLink. Simply,I wanted to add the "HyperLink" column to my existing ContentType.
                SPWeb web = SPContext.Current.Web;
                SPContentTypeId cId = new SPContentTypeId("0x0100AF98B284ECB44322916F1A1AD1A9E31C");
                SPContentType cType = new SPContentType(cId, web.ContentTypes, "MurugesanCType");
                web.Fields.Add("T2", SPFieldType.URL, true);
                SPField f = web.Fields["T2"];
                f.Description = "Type your Org NAme";
                web.ContentTypes[cId].FieldLinks.Add(new SPFieldLink(f));
                web.ContentTypes[cId].Update();

Monday, April 15, 2013

Adding Taxonomy Field to Content Type

    
            SPWeb web = SPContext.Current.Web;
            SPContentTypeId cId = new SPContentTypeId("0x0100AF98B284ECB44322916F1A1AD1A9E31C");
            SPContentType cType = new SPContentType(cId, web.ContentTypes, "MurugesanCType");
            web.ContentTypes.Add(cType);
            Guid termSetId;
            TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
            TermStore store = session.TermStores[0];
            Group group = store.Groups[1];
            TermSetCollection tCols = group.TermSets;
            foreach (TermSet set in tCols)
            {
              if(set.Name.ToString().Equals("Role"))
              {
                 termSetId = set.Id;
                 TaxonomyField tField = web.Fields.CreateNewField("TaxonomyFieldType", "Profile Role") as TaxonomyField;
                 tField.SspId = set.TermStore.Id;
                  tField.TermSetId = termSetId;
                  tField.TargetTemplate=string.Empty;
                  tField.AllowMultipleValues=false;
                  tField.CreateValuesInEditForm=true;
                  tField.Open=true;
                  tField.AnchorId=Guid.Empty;
                  tField.Group = "Meta Data - Custom";
                  web.Fields.Add(tField);
                  SPField spField = web.Fields["Profile Role"];
                  web.ContentTypes[cId].FieldLinks.Add(new SPFieldLink(spField));
                  web.ContentTypes[cId].Update();
                
              }
            }
            Web.Update();