Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to move a term from one parent to another using code.

My exact code is:

term.Move(newParentTerm);
termStore.CommitAll(); 

I can't spot anything obvious that is wrong, no error message is thrown, except the term doesn't move it just stays where it is.

Any suggestions?

EDIT: Tried the code on another server, and the same problem occurred. No errors, just nothing happened.

EDIT 2: Full code block coming in :

public void MoveTermFromCustomProperty(string termStoreName, string groupName, string termSetName, KeyValuePair<string, string> customProp, KeyValuePair<string, string> newParentIDProp)
        {
            using (SPSite spSite = new SPSite(this.SiteAddress))
            {
                TaxonomySession session = new TaxonomySession(spSite);
                TermStore termStore = session.TermStores.Where(ts => ts.Name == termStoreName).FirstOrDefault();
                Group group = termStore.Groups.Where(g => g.Name == groupName).FirstOrDefault();
                TermSet termSet = group.TermSets.Where(tset => tset.Name == termSetName).FirstOrDefault();



                Term term = GetTermFromCustomProperty(termStoreName, groupName, termSetName, customProp);

                if (term != null)
                {
                    if (newParentIDProp.Value == "00000000")
                    {
                        // Wants to append to the termset
                        if (!term.IsRoot)
                        {
                            term.Move(termSet);
                            termStore.CommitAll();
                        }
                    }
                    else
                    {
                        // A possible move to new parent term.
                        Term newParentTerm = this.GetTermFromCustomProperty(termStoreName, groupName, termSetName, newParentIDProp);
                        if (newParentTerm.Id != term.Id && newParentTerm.Id != term.Parent.Id)
                        {
                            term.Move(newParentTerm);
                            termStore.CommitAll(); 
                        }
                     }
                }
                else
                {
                    throw new Exception("Term cannot be null TODO add exception details");
                }
            }
        }
share|improve this question
    
could you provide us with a stack trace? Also did you try debugging the program? –  Anders Rask Apr 28 '11 at 18:10
    
I debugged it line by line, there was no error, and what is interesting if you do a query in intermediate window for term.parent.name it seems correct, however nothing reflects back in Sharepoint only old structure. This is after the commitAll(). –  JL01 Apr 28 '11 at 18:51
    
Going to try restart the server.... Also tried to update the name property and that also didn't reflect.... –  JL01 Apr 28 '11 at 18:56
    
Did nothing.... –  JL01 Apr 28 '11 at 19:09
    
Could you post the complete code listing for this block? –  Tim Ebenezer May 3 '11 at 9:02
show 3 more comments

4 Answers 4

Are you running the console app as the intended user ? I've found that it is sometimes needed, as impersonation is tricky in console apps.

Also: to clear the obvious question : are you able to perform this operation manually ? (e.g does it occur on all terms/src/destination, or just on a specific one ? )

How does the system behave if you do the same through powershell ?

I ran the following (using the SharePoint2010 Management Shell as a Farm Admin) :

    $site = Get-SPSite $url
    $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
    $termstore = $session.TermStores["MyTermStore"]
    $group = $termstore.Groups["MyGroup"]
    $termset = $group.TermSets[0]
    $srcpath= $termset.Terms["srcpath"]
    $destpath= $termset.Terms["destpath"]
    $term = $srchpath.Terms["myterm"]
    $term.Move($destpath)
    $termStore.CommitAll()

(replace with your terms obviously) - and it worked out ok.

share|improve this answer
    
Will check what powershell does, thanks –  JL01 May 2 '11 at 20:45
add comment
up vote 1 down vote accepted

We've opened up a Gold Support Ticket with Microsoft on this issue.

So far the Microsoft Support Specialist has determined that it could be an issue with the language pack installed on the server.

I will update this answer once we know more and get a solution / workaround.

OK PROBLEM FOUND.

I had a main function with branches to sub routines responsible for finding terms. In the sub routines I was creating a whole new instance of SPSite, and TermStore. This means even though I got a reference to a term and passed it back to the main funtion.

The main function had its own instance of termStore, so making changes to the term returned from a subroutine were never committed because technically the term had another instance of termstore which was never getting called.

To solve this problem, ensure you keep all your methods on the same TaxonomySession. Pass it through to subroutines if you have to.

As per my code above:

Term term = GetTermFromCustomProperty(termStoreName, groupName, termSetName,CustomProp); //WRONG NO SESSION PASSED THROUGH TO THIS SUBROUTINE 

Term term = GetTermFromCustomProperty(termStoreName, groupName, termSetName, customProp, session); // CORRECT - Contains a session

Then inside the sub routine WORK WITH THE SAME SESSION!:

public Term GetTermFromCustomProperty(string taxonomyTermStoreName, string taxonomyGroupName, string taxonomyTermSetName, KeyValuePair<string, string> objIDProperty, TaxonomySession session)
{

        TermStore termStore = session.TermStores.Where(ts => ts.Name == taxonomyTermStoreName).FirstOrDefault();
        Group group = termStore.Groups.Where(g => g.Name == taxonomyGroupName).FirstOrDefault();
        TermSet termSet = group.TermSets.Where(tset => tset.Name == taxonomyTermSetName).FirstOrDefault();

        foreach (var topTerm in termSet.GetAllTerms())
        {
            if (topTerm.CustomProperties.Keys.Contains(objIDProperty.Key))
            {
                if (topTerm.CustomProperties[objIDProperty.Key] == objIDProperty.Value)
                {
                    return topTerm;
                }
            }
        }

    return null; 
}
share|improve this answer
add comment

Are you running this inside a console app? If so how do you establish context and what permissions are the user having? Check if user is farm admin and has full control in administrative permissions on Service Application

share|improve this answer
    
Running from console app - using - using (SPSite spSite = new SPSite(this.SiteAddress)) { TaxonomySession session = new TaxonomySession(spSite); to generate context... –  JL01 Apr 28 '11 at 22:28
    
There is another overload on the TaxonomySession constructor which forces a cache update on the session context. It may be helpful to run your code and generate a new session from the updated cache after the commit. –  ciphertxt May 2 '11 at 1:37
    
Tried that thanks, didn't work –  JL01 May 2 '11 at 11:45
add comment

Does the user account has Term Store Administrators right? (under Term Store Management Tool > Term Store Administrators)

share|improve this answer
    
Yes, checked this too during the run, and no error message is thrown. Using redgate reflector VS plugin, I stepped through the taxonomy dll code, and it runs everything without throwing a single error, only nothing happens. –  JL01 Apr 28 '11 at 23:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.