Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Add "I like it" tag programmatically

$
0
0

Hi everybody

I have a case that I need to add "I like it" tag programmatically but for another user (not the logged in one).

impersonation seems not working!

private void SetILikeIt(string userLoginName)
        {
            Uri myUri = new Uri("http://epm10:2011/Pages/test.aspx");
            HttpContext tempCtx = HttpContext.Current;
            SPUser usr = SPContext.Current.Web.AllUsers[userLoginName];
            HttpContext.Current = null;
            using (SPSite site = new SPSite("http://epm10:2011", usr.UserToken))
            {
                using (SPWeb spWeb = site.OpenWeb())
                {
                    GetServiceContext();
                    //SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
                    //SocialTagManager stm = new SocialTagManager(context);
                    //Retrieve the taxonomy session from the SocialTagManager.                    
                    //Term myTerm = new Term(); stm.TaxonomySession.DefaultKeywordsTermStore.KeywordsTermSet.Terms["I like it"];
                    TaxonomySession txs = stm.TaxonomySession;
                    TermStore ts = txs.DefaultKeywordsTermStore;
                    //Term term = null;
                    TermCollection terms = ts.KeywordsTermSet.GetTerms("I like it", ts.DefaultLanguage, true);
                    Term t = null;
                    if (terms.Count == 0)
                    {
                        t = ts.KeywordsTermSet.CreateTerm("I like it",ts.DefaultLanguage);
                        ts.CommitAll();
                    }
                    else
                        t = terms[0];
                    //
                    SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                t.Owner = usr.LoginName;
            });
                    stm.AddTag(myUri, t, myUri.AbsoluteUri);
                }
            }
            HttpContext.Current = tempCtx;
        }

it still adding the tag for the current logged in user?

Viewing all articles
Browse latest Browse all 11508

Trending Articles