How to create a site collection taxonomy group programmatically?
-
Tuesday, July 27, 2010 8:29 AM
Hi,
My application needs to create a site collection group.
I tried TermStore.CreateSiteCollectionGroup, but got error "method not found" (and is not listed in MSDN).
I also tried Group.IsSiteCollectionGroup = true, but it raised an exception (and MSDN says it's not supported).So how can I do it?
Thanks in advance
Answers
-
Tuesday, August 03, 2010 3:17 PM
It cannot be done programatically. The TermStore.CreateSiteCollectionGroup method is internal and used by the internal SharePoint object model. There is a great amount of security that surrounds the creating of a SiteCollection Group for a taxonomy.
certdev.com- Marked As Answer by Sruli I.Ganor Tuesday, August 03, 2010 3:24 PM
-
Friday, February 25, 2011 3:26 PM
The method is internal, so you cannot call it directly from your code. A workaround is to call it by reflection. This way it is possible to create a Taxonomy SiteCollection Group programmaticaly.
Type storeType = typeof(TermStore); Type[] methodParameterTypes = new Type[] { typeof(string), typeof(SPSite)}; object[] parameters = new object[]{name, site}; MethodInfo creationMethod = storeType.GetMethod("CreateSiteCollectionGroup", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic, null, methodParameterTypes, null); Microsoft.SharePoint.Taxonomy.Group newGroup = (Microsoft.SharePoint.Taxonomy.Group) creationMethod.Invoke(termStore, parameters);
- Proposed As Answer by Patrick Stalljohann Friday, February 25, 2011 3:26 PM
- Marked As Answer by Sruli I.Ganor Monday, February 28, 2011 9:14 AM
All Replies
-
Tuesday, July 27, 2010 8:39 AM
Hi refer the below code snippet
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb) { SPGroupCollection collGroups = oWebsiteRoot.SiteGroups; SPUser oUser = oWebsiteRoot.Users["User_Name"]; SPMember oMember = oWebsiteRoot.Users["User_Name"]; collGroups.Add("Group_Name", oMember, oUser, "Description"); }
---
Rajesh | My Blog
MCTS - WSS AD, WSS Config, MOSS Config. -
Tuesday, July 27, 2010 12:03 PM
Rajesh, sorry but you are talking about adding a SPGroup, while I'm asking about Taxonomy group (Microsoft.SharePoint.Taxonomy.Group).
I need to add a group to a term store. However this should be a group that is private to a certain site collection.
There are 2 ways to do it, neither of them works.
Sruli
-
Tuesday, July 27, 2010 12:48 PM
The taxonomy functionality was designed (and partially spawned) specifically to allow sharing across site collections. But you can obviously control who gets rights to the term store group by adding certain SharePoint/AD groups.
I wrote a blog series about SharePoint 2010 Enterprise Metadata Management (EMM). aka taxonomy. This is from one of the dev posts: http://geeklit.blogspot.com/2009/12/sharepoint-managed-metadata-developer.html
using (SPSite site = new SPSite("http://localhost/")) { //Instantiates a new TaxonomySession for the current site. TaxonomySession session = new TaxonomySession(site); //Instantiates the connection named "Managed Metadata Service" for the current session. TermStore termStore = session.TermStores["Managed Metadata Service"]; Group group = termStore.CreateGroup("Africa"); TermSet termSet = group.CreateTermSet("South Africa"); Term term = termSet.CreateTerm("Cape Town", 1033); term.SetDescription("This is the city term for Cape Town", 1033); term.CreateLabel("Cape of Good Hope", 1033, false); Term termChild = term.CreateTerm("Newlands", 1033); termChild.Delete(); termStore.CommitAll(); Label1.Text = " Finished"; }
- cawood
blog | twitter -
Tuesday, July 27, 2010 3:32 PM
Stephen, I'm glad that your reply gave me the opportunity to access your blog and find such useful info.
However, this still doesn't answer my question, so I'll explain it in more detail:
When you create a metadata column, you can select this option:
o Customize your term set:
A custom term set will be available to other users in the site collection, however its terms will not be offered as suggestions in Enterprise Keywords columns.Selecting it creates a termset that is local to your site collection.
Assuming the site collection is http://lab01/sites/tax,
SP puts all the termsets that are local to this site collection in a taxonomy group called lab01-sites-tax.
When opening MMS from Central Administration -> Manage Service Application, you don't see this group.
In the group object, flag Group.IsSiteCollectionGroup is true.Now, my application needs to create such a group that shuld be local to a certain site collection.
Unfortunately I can only create it as a farm-level group, that is available to all the farm's sites (including its termsets), and appears in central admin.So the issue is not limiting the people with access to the group, but limiting the group's scope to a given site.
I've tried 2 ways, both raied exceptions:
TermStore.CreateSiteCollectionGroup (not implemented)
Group.IsSiteCollectionGroup = true (read only property)What is the right way?
Thanks in advance
-
Tuesday, August 03, 2010 2:24 PM
Nobody knows? :-(
-
Tuesday, August 03, 2010 3:17 PM
It cannot be done programatically. The TermStore.CreateSiteCollectionGroup method is internal and used by the internal SharePoint object model. There is a great amount of security that surrounds the creating of a SiteCollection Group for a taxonomy.
certdev.com- Marked As Answer by Sruli I.Ganor Tuesday, August 03, 2010 3:24 PM
-
Tuesday, August 03, 2010 3:27 PM
Oh dear, this is bad news...
Why is creating site collection taxonomy group more secured than creating a public (farm-wise) taxonomy group, which I can easily do?
Is there any workaround, e.g. PowerShell?
Thanks anyway.
-
Friday, February 25, 2011 3:26 PM
The method is internal, so you cannot call it directly from your code. A workaround is to call it by reflection. This way it is possible to create a Taxonomy SiteCollection Group programmaticaly.
Type storeType = typeof(TermStore); Type[] methodParameterTypes = new Type[] { typeof(string), typeof(SPSite)}; object[] parameters = new object[]{name, site}; MethodInfo creationMethod = storeType.GetMethod("CreateSiteCollectionGroup", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic, null, methodParameterTypes, null); Microsoft.SharePoint.Taxonomy.Group newGroup = (Microsoft.SharePoint.Taxonomy.Group) creationMethod.Invoke(termStore, parameters);
- Proposed As Answer by Patrick Stalljohann Friday, February 25, 2011 3:26 PM
- Marked As Answer by Sruli I.Ganor Monday, February 28, 2011 9:14 AM
-
Monday, February 28, 2011 9:46 PM
Thanks Patrick. Great idea.
This does the job, especially if you add termStore.CommitAll() at the end.
I wonder if we can use reflection to call an internal method in a commercial software product.
At the moment it seems that there is no alternative.Thanks again.

