Pages

Showing posts with label Enterprise Metadata Magement (EMM). Show all posts
Showing posts with label Enterprise Metadata Magement (EMM). Show all posts

Friday, December 31, 2010

SharePoint 2010: Taxonomy Event Receiver?

One of the power of SharePoint is extensibility. You can hook your custom code in different places in different time with Event Receiver. Taxonomy is new and powerful feature added in SharePoint 2010. Unfortunately, the taxonomy missing event receiver feature. If you want to do something when taxonomy added, deleted or updated, you are unlucky that SharePoint 2010 doesn’t provide event receiver in SharePoint 2010 for taxonomy.

 

Why taxonomy event receiver needed?

With taxonomy support, SharePoint is a good place to manage taxonomy. However, if I need this taxonomy to use in another application (like another asp.net application) then with the event receiver I could sync my asp.net application with SharePoint taxonomy. Also when a taxonomy is deleted I would like to run my own code to allow or disallow the deletion. So taxonomy event receiver would be very handy if it would be available.

 

How taxonomy managed in SharePoint?

Let’s discuss a bit about how taxonomy managed in SharePoint. Taxonomy is managed by central administration (with managed metadata service). So when you add/delete/update taxonomy, the taxonomies are managed in central administration site. However, for faster retrieval of taxonomies in individual web, a hidden list of taxonomies maintained in each site collection. So in each site collection, there’s a hidden list TaxonomyHiddenList that keeps the copy of taxonomies from central administration site. And in every hour a timer job “Taxonomy Update Scheduler” is run to sync the taxonomies between central admin and site collection. However, the hidden list in site collection doesn’t contain all taxonomies from central admin rather taxonomies that are used in the webs of the site collection. So the hidden taxonomy list have only keywords and taxonomies used in the webs of the site collection. You can get the contents of the hidden list by browsing “http://mysitecollection/Lists/TaxonomyHiddenList” where mysitecollection is your site collection url. Few things to notice for the hidden list:

  • In case of edit mode, the taxonomy is read from central administration and as user save the list item, the taxonomy/keywords is saved from hidden list.
  • In case of viewing an item, it’s for sure that the taxonomy/keyword is in hidden list (it was put in hidden list during edit mode). So the taxonomy/keyword is shown from hidden list.

 

Event Receiver for Taxonomy Hidden List

Now we know the taxonomies are kept in a hidden list at site collection level. And in every hour,  the hidden list is synchronized with central administration site. So if we add event receiver for the hidden taxonomy list,then in every hour our event receiver will be fired while the hidden list will be synchronized. So adding an event receiver for hidden taxonomy list will solve the problem apparently. But there’s still problem. Not all taxonomies are added to the hidden list from central administration. Only the taxonomies added to subsites of site collection, are added to the hidden list. Now the problem of event receiver for taxonomies can be divided into two categories:

  • Need event receiver only for taxonomies used in subsite of site collection: If you need the event receiver only for taxonomies used in subsites of site collection, then having the event receiver for hidden taxonomies will do. However, you’ll have to wait for one hour to fire the event receiver.
  • Need event receiver for all taxonomies: If you need your event receiver to be fired for every taxonomy (added/edited/deleted) manipulation, then tapping the hidden taxonomy list will not work as not all taxonomies will be added from central admin to hidden taxonomy list. So what might be the solution for this? One solution is to create a custom timer job of your own and in that timer job, use all taxonomies in a test list. Since all taxonomies are used in your site, so all taxonomies will be synchronized with the site collection and your event receiver will be fired for all taxonomies.
 

Solution (Re-explained)

so if only care for taxonomies used in the site collection then adding an event receiver for hidden list will work. In every hour the hidden taxonomy lists will be synchronized and your event receiver for that hidden list will be fired.

However, if you need to care all taxonomies (not just taxonomies used in the site collection), then you need to follow the steps:

  1. Create a test list with one metadata field.
  2. Create a timer job to add all metadata from central admin to the list’s metadata field. The purpose of this job is make sure all taxonomies from central admin are used in site collection.
  3. Now add the event receiver for hidden list and you are done.

Tuesday, December 14, 2010

SharePoint 2010: Create Taxonomy Error “Term set update failed because of save conflict.”

I was trying to answer a user’s problem in MSDN forum. He was trying to add a term in term store. Using the term adding code (that I have taken from the post) I was getting the error “Term set update failed because of save conflict.” while I was calling the CommitAll method.

 

Problematic Code

My code is as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }

}

After investigating the code I had found the problem was in the line as shown in the above code snippet with yellow marker (termSet.IsOpen..).

 

Solution

I have fixed the error by modifying the code as shown below. I had just called the CommitAll just after changing the value of IsOpenForTermCreation as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
                termStoreTerm.CommitAll();
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }
}

 

Conclusion

So the problem was that when I changed the value of IsOpenForTermCreation, there was a commit pending. So without committing, I created a new term and tried to committed. So the save conflict error was thrown. If you get the same error in different scenario then u can check if u have any pending commit that u have not committed.

Wednesday, March 24, 2010

SharePoint 2010: Share Content Type with Managed Metadata Service

Till SharePoint 2007 there’s no way to manage Content type centrally. To share Content Type across site collection level or web application level in SharePoint 2007, you need to copy the same content type in different site collections or web applications. Another way could be to use web feature to enable content type across site collections or web applications.But the good news is that SharePoint 2010 has provided out of the box support for sharing Content types across farm level.

Publish Content Type

The Enterprise Metadata Management Service or Metadata Service (in short), allows to share Metadata and Content Type across the farm. To share Content types you need to define the content type(s) in a site collection (say CTSiteCollection). You can consider the site collection (CTSiteCollection) as the place where you’ll put all your related content types to share across farm. This site collection,which is used to host content types or sharing,is called Content Type Hub.

1. Create a new content type in a site collection that you want to share

First select a site collection as a place to host your content types. You will define your content types to share across, in this site collection. You can create a new site collection or use an existing one. Once you have an site collection in your hand, create a content type and for this discussion let’s name it MyContentType.

2. Activate feature to enable content publishing in site collection:

The site collection, which will host content types for sharing, is known as content type hub. To enable your site collection to be a content type hub you need to  activate a feature known as “Content Type Syndication Hub”. Once you have activated this feature, you will find an option “Manage Publishing for this content type” in your content type setting page as shown below:

image

Figure 1: Content type publishing option will be available on activating feature “Manage Publishing for this content type”

3. Bind content type hub with a metadata service.

Go to the central administration => Application Management. Then select your metadata service or create a new one. You will find there are two entries for each metadata service. One is service and another is connection as shown below.

image

Figure 2: Metadata service and connection

Configure Metadata Service: Now click on the metadata service (but not on the name as this will bring you to ‘Term Store Management Tool’) and then click Properties button from the ribbon on the top of the page. On the properties window there’s an option for Content Type Hub and here put the url of your site collection you have selected/created (for example, http://server:port/ or http://server:port/sites/mysite). Now your metadata service is ready to be used by metadata service

Configure Metadata Connection: To configure metadata connection, go to metadata service connection properties and ensure that the connection consumes the content type hub as shown below:

image

Figure 3: Metadata connection settings for consuming content type hub.

4. Publish the content type

Now you go back to the site collection (which is used as content type hub) and go to content type settings page. Here in this page you will get the option “Manage publishing for this content type” option. If you don’t get the option activate feature “Content Type Syndication Hub” as described in step 2. Now click on the link “Manage Publishing for this content type” and ensure the ‘publish’ option is selected and then click OK as shown below.

image

Figure 4: Content type publishing page

Now you are done but you may not find the content type available immediately as the publishing is managed by a timer job which usually run in every 15 minutes. To run the job immediately you can navigate to Central Admin site => Monitoring => Review Job definitions. Then you can run the ‘Content Type Hub’ immediately.

 

Use Published Content Type

1. Associate Metadata connection to your web application:

To use published content type from a different web application or site collection, you need to associate the metadata connection with the web application. To do so navigate to the Central Administration => Application Management => Configure service application associations. Then click on the site link and you’ll be provided an “Configure Service Application Associations” window. From there ensure you have selected the metadata service which you configured for content type.

After associating the metadata service with web application, you may not get the content type immediately as a timer job will sync the content type between content type hubs and consumers. To run the job immediately navigate to Central Admin site => Monitoring => Review Job definitions. Then find the job “Content Type Subscriber”. There are one job for each subscriber. Find the job for your site and run immediately.

2. Use Content Type

To use content type go to the site where you want to use the content type. But before that make sure you have associated the metadata connection for this web application as described in step 1. Now create a new list in the consumed web site and go to list settings. Then click ‘Advanced settings’ and select yes for ‘Allow Management of Content Types’. Now click ok and move back to settings page and you should get the ‘Content Types’ option as shown below:

image

Figure 5: Content Type settings for list/library

Now you can click on ‘Add from existing site Content Types’ and you should get the consumed content type ‘MyContentType’ as we created at step 1 under section ‘Publish Content Type’. If you don’t find the content type defined in content type hub site, there’s something wrong. To troubleshoot the problem move to the next section.

 

Troubleshoot, if Content type is not available in the consumed web site

To find if there’s any problem in publishing content type, go to site where you have defined the content type. Then go to site settings => “Content type Publishing“. Here you will find options for viewing error log. Though the error log is not so much user friendly but you may get the gist of problem.

If there’s nothing in the error log then make sure the timer job (which actually causes the content types to publish/consume) is run. Timer jobs run periodically, may be after 15 minutes. To run the job immediately, go to Central Administration => Monitoring => Review Job Definitions. Find all jobs related to Content types. After running all jobs check if content type is available. If not available then search for error in the error log as described in previous paragraph.

Saturday, March 20, 2010

SharePoint 2010: Hierarchical and Equivalent taxonomy types are supported

If you don’t have basic idea of taxonomy terms in SharePoint please read my previous related article. As we know SharePoint 2010 has added support for taxonomy with a feature called Enterprise Metadata Management or EMM. There are three basic kids of relationship in taxonomy:
1. Hierarchical: In this type of relation, terms (items in taxonomy) are connected hierarchically. For example Books can be categorized as Novel, Drama, Science Fiction etc. Then Novel can be subcategorized in different ways. So these represent hierarchical relationship in taxonomy.
2. Equivalent: Two terms can be equivalent. For example TV and Television are equivalent.
3. Associative: In this case terms are related anyway. For example Television and Radio are related in some context.

How taxonomy types/relations are used in SharePoint?

Taxonomy is used mainly to tag content but how these three taxonomy types are used in SharePoint for tagging content. For your information only Hierarchical and Equivalent content types are supported in SharePoint.  For example you are uploading a video in a site developed with SharePoint. You want to tag the video with metadata so that user can find the video easily using search functionality. While you will upload the video you can select from existing term from taxonomy or enter a new/existing term(s) for this video. Say you are uploading a BBC TV program on famine in Africa. Then you can select/enter terms: TV, BBC, Famine, Africa. Now consider how we can organize these terms with taxonomy supports in SharePoint. Take a look at the figure 1 where you can find three sections:
1. Hierarchical taxonomy section: User will select from a predefined set of terms from taxonomies.
2. Upload video section: Upload the content, i.e., video.
3. Keyword (equivalent taxonomy) section: User will enter and as he/she will enter, suggestions will come up. As you can see in the image below, while user is typing BBC suggestion is coming up with British Broadcasting Service (BBC). Here BBC and British Broadcasting Service are equivalent term in the taxonomy. So whether user enter BBC or British Broadcasting Service, a single term (may be BBC or British Broadcasting Service) will be selected. So if another user search video by BBC or British Broadcasting Service, this will be selected as BBC and British Broadcasting Service are the same.
image
Figure 1: How Hierarchical and Equivalent taxonomy can be used to tag content (i.e., video)

How to define Hierarchical and Equivalent terms in SharePoint 2010

To define a term in SharePoint 2010, go to Center administration site and navigate to ‘Application Management’. Then navigate to ‘Manage Service Applications’ under Service Applications. Then select the metadata service and you will be navigated to ‘Term Store Management Too’. You can easily define hierarchical terms. However to define any Equivalent terms, define any equivalent term first. For our example of ‘Television’, define Television first. Then you will find an option for defining equivalent term for television where you can define TV.
image
Figure 2: Define Equivalent Term
As shown in the figure 2, you can define multiple equivalent terms for one term.

Conclusion

I had read in few blogs that SharePoint 2010 supports hierarchical and equivalent taxonomy but I had not found how equivalent term is defined. In this post I have tried to show how you can define the equivalent as well as hierarchical terms with term management tool.