Wednesday, 28 December 2011

manipulating termstore using visual webpart


SharePoint 2010 -Manipulating Term Store using Visual Web Part


Summary of Steps

Step 1: Create the SharePoint Project. In this step, create a Microsoft Visual Studio 2010 project                                       that you can use to deploy and test your custom action menu.
Step 2: Create a Visual WebPart.in this step create visual web part used for customizing purpose and it can be deployed to the sites.
Step 3: Add user control
Step 4: Coding for manipulating term store using visual webpart


Step 1: Create the SharePoint Project

Open Visual Studio 2010 >File > New >Project >SharePoint 2010>Empty SharePoint Project. >Name it Custom Menu in List Settings>Ok >and select deploy as farm solution>give site name>finish.



Step 2: Create a Visual Web Part

In the solution explorer right click on the solution >add>new item>Visual Web Part>give name for that Visual Web Part>ok

Step 3: Add user control

In the solution explorer click the designing part of the user control like” meta data usercontrol.ascx” writes html coding as like that of below

Step 4: Add references

In the Solution explorer right click on references>add references>select Microsoft. SharePoint, Microsoft.SharePoint.Taxonomy.

Step5: Coding for manipulating term store using visual Web Part

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace termstore.metadata
{
    public partial class metadataUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPSite thisSite = SPContext.Current.Site;
            TaxonomySession session = new TaxonomySession(thisSite);
            TreeNode treeNode = new TreeNode();
            treeNode.Text = "Metadata Awesomeness";
            metadatatree.Nodes.Add(treeNode);
            foreach (TermStore termStore in session.TermStores)
            {
                var tsNode = new TreeNode(termStore.Name, null, null, "", null);
                treeNode.ChildNodes.Add(tsNode);
                //treeNode = tsNode;
                foreach (Group group in termStore.Groups)
                {
                    var node = new TreeNode(group.Name, null, null, "", null);
                    treeNode.ChildNodes.Add(node);
                    //treeNode = node;

                    foreach (TermSet termSet in group.TermSets)
                    {
                        node = new TreeNode(termSet.Name, null, null, "", null);
                        treeNode.ChildNodes.Add(node);
                        treeNode = node;
                        foreach (Term term in termSet.Terms)
                        {
                            AddTermSet(term, treeNode);
                        }
                    }
                }
            }
        }
        void AddTermSet(Term term, TreeNode treeNode)
        {
            var node = new TreeNode(term.Name, null, null, "", null);
            treeNode.ChildNodes.Add(node);
            treeNode = node;
            foreach (Term t in term.Terms)
            {
                AddTermSet(t, treeNode);
            }
        }
    }
  
}




Result


References

http://www.zimmergren.net/technical/sp-2010-introduction-to-programmatically-working-with-taxonomies-in-sharepoint-server-2010

No comments:

Post a Comment