private void FollowingAndStopFollowing() { //SPUser us = SPContext.Current.Web.CurrentUser; SPSocialFollowingManager _followManager = null; //SPSecurity.RunWithElevatedPrivileges(delegate() //{ //using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.Url )) //{ //SPSite elevatedSite = new SPSite(SPContext.Current.Site.Url); //SPServiceContext serverContext = SPServiceContext.GetContext(SPContext.Current.Site); //UserProfileManager profileManager = new UserProfileManager(serverContext); //UserProfile profile = profileManager.GetUserProfile(us.LoginName); //if (profile != null) //{ //Create a Social Manager profile _followManager = new SPSocialFollowingManager();//profile); SPSocialActorInfo actorInfo = null; //SPSocialActor[] actors = _followManager.GetFollowers(); SPSocialActor[] ifollow = _followManager.GetFollowed(SPSocialActorTypes.Sites); //foreach (SPSocialActor follower in ifollow) //{ actorInfo = new SPSocialActorInfo(); actorInfo.ContentUri = new Uri(txtDocUrl.Text);// follower.Uri; actorInfo.ActorType = SPSocialActorType.Site; SPSocialFollowResult result; if (!_followManager.IsFollowed(actorInfo)) { result = _followManager.Follow(actorInfo); this.Controls.Add(new LiteralControl("follow: " + result.ToString())); } else { bool r = _followManager.StopFollowing(actorInfo); this.Controls.Add(new LiteralControl("stoppFollowing " + r.ToString())); } //} //} //} //}); }
//最新的关注的网站和取消的网站 private void FollowingAndStopFollowing() { SPUser us = SPContext.Current.Web.CurrentUser; SPSocialFollowingManager _followManager = null; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite elevatedSite = new SPSite(SPContext.Current.Web.Url)) { SPServiceContext serverContext = SPServiceContext.GetContext(SPContext.Current.Site); UserProfileManager profileManager = new UserProfileManager(serverContext); UserProfile profile = profileManager.GetUserProfile(us.LoginName); if (profile != null) { //Create a Social Manager profile _followManager = new SPSocialFollowingManager(profile); SPSocialActorInfo actorInfo = null; //SPSocialActor[] actors = _followManager.GetFollowers(); SPSocialActor[] ifollow = _followManager.GetFollowed(SPSocialActorTypes.Sites); foreach (SPSocialActor follower in ifollow) { actorInfo = new SPSocialActorInfo(); actorInfo.ContentUri = follower.Uri; actorInfo.ActorType = SPSocialActorType.Site; if (!_followManager.IsFollowed(actorInfo)) { _followManager.Follow(actorInfo); des.Text = "follow:OK"; } else { _followManager.StopFollowing(actorInfo); des.Text = "stoppFollowing OK"; } } } } }); }
//"以下代码示例使当前用户开始关注或停止关注某目标项目" void GuanZhuAndStopGuanZhu() { des.Text = ""; // Replace the following placeholder values with the URL of the target // server and target document (or site). string serverUrl = txtSiteUrl.Text; string contentUrl = txtDocUrl.Text; SPSocialActorType contentType = SPSocialActorType.Site; //WriteFollowedCount(contentType); using (SPSite site = new SPSite(serverUrl)) { using (new Microsoft.SharePoint.SPServiceContextScope(SPServiceContext.GetContext(site))) { SPServiceContext serviceContext = SPServiceContext.GetContext(site); UserProfileManager upm = new UserProfileManager(serviceContext); ////Create user sample string sAccount = "mydomain\\myalias"; ////To set prop values on user profile string accountName = SPContext.Current.Web.CurrentUser.LoginName; accountName = accountName.Substring(accountName.LastIndexOf("|") + 1); //if (accountName == "SHAREPOINT\\system") // accountName = "ccc\\xueqingxia"; ////UserProfile u = upm.GetUserProfile(accountName); followingManager = new SPSocialFollowingManager();//u,serviceContext); int followedCount = followingManager.GetFollowedCount(SPSocialActorTypes.Sites); des.Text = "关注的网站的个数:" + followedCount.ToString(); // Create a SocialActorInfo object to represent the target item. SPSocialActorInfo actorInfo = new SPSocialActorInfo(); actorInfo.ContentUri = new Uri(contentUrl); actorInfo.ActorType = contentType; //actorInfo.AccountName = accountName; // Find out whether the current user is following the target item. bool isFollowed = followingManager.IsFollowed(actorInfo); des.Text += " isFollowed:" + isFollowed.ToString(); try { //SPSocialFollowResult result = followingManager.Follow(actorInfo); //// If the result is AlreadyFollowing, then stop following //// the target item. //des.Text += "FollowResult:" + result.ToString(); //if (result == SPSocialFollowResult.AlreadyFollowing) //{ if (isFollowed) { des.Text += " StopFollowing: "; bool isToped = followingManager.StopFollowing(actorInfo); des.Text += isToped.ToString(); } else { des.Text += " Follow: "; SPSocialFollowResult result = followingManager.Follow(actorInfo); // If the result is AlreadyFollowing, then stop following // the target item. des.Text += "FollowResult:" + result.ToString(); } } catch (Exception ex) { des.Text += ex.ToString(); } } } }