private void ClientGuanZhu() { SPSite site = SPContext.Current.Site;//(txtSiteUrl.Text)) SPServiceContext serviceContext = SPServiceContext.GetContext(site); UserProfileManager upm = new UserProfileManager(serviceContext); string accountName = SPContext.Current.Web.CurrentUser.LoginName; accountName = accountName.Substring(accountName.LastIndexOf("|") + 1); des.Text = ""; if (accountName == "sharepoint\\system") { accountName = "ccc\\xueqingxia"; } if (upm.UserExists(accountName)) { UserProfile u = upm.GetUserProfile(accountName); Response.Write(u[PropertyConstants.PictureUrl].Value); SPSocialFollowingManager follow = new SPSocialFollowingManager(u, serviceContext); SPSocialActorInfo socialInfo = new SPSocialActorInfo(); des.Text = follow.GetFollowedCount(SPSocialActorTypes.Sites).ToString(); foreach (SPSocialActor spFollow in follow.GetFollowed(SPSocialActorTypes.Sites)) { //spFollow.Status = SPSocialStatusCode.InternalError; socialInfo.ContentUri = spFollow.Uri; //socialInfo.AccountName = u.AccountName; socialInfo.ActorType = SPSocialActorType.Site; follow.StopFollowing(socialInfo); des.Text += "ok! "; } } }
//以下代码示例获取当前用户正在关注的文档和网站并获取有关用户的关注内容状态的信息 void GetGuanZhu() { // Replace the following placeholder values with the URLs of // the target server, document, and site. string serverUrl = txtSiteUrl.Text; //string docContentUrl =txtDocUrl.Text ; string siteContentUrl = txtDocUrl.Text; // do not use a trailing '/' for a subsite // Get the client context. //ClientContext clientContext = new ClientContext(serverUrl); // Get the SocialFollowingManager instance. SPSocialFollowingManager followingManager = new SPSocialFollowingManager(); // Create SocialActorInfo objects to represent the target // document and site. //SPSocialActorInfo docActorInfo = new SPSocialActorInfo(); //docActorInfo.ContentUri = new Uri(docContentUrl); //docActorInfo.ActorType = SPSocialActorType.Site; SPSocialActorInfo siteActorInfo = new SPSocialActorInfo(); siteActorInfo.ContentUri = new Uri(siteContentUrl); siteActorInfo.ActorType = SPSocialActorType.Site; // Find out whether the current user is following the target // document and site. //ClientResult<bool> isDocFollowed = followingManager.IsFollowed(docActorInfo); bool isSiteFollowed = followingManager.IsFollowed(siteActorInfo); // Get the count of documents and sites that the current // user is following. //ClientResult<int> followedDocCount = followingManager.GetFollowedCount(SocialActorTypes.Documents); int followedSiteCount = followingManager.GetFollowedCount(SPSocialActorTypes.Sites); // Get the documents and the sites that the current user // is following. //ClientResult<SocialActor[]> followedDocResult = followingManager.GetFollowed(SocialActorTypes.Documents); SPSocialActor[] followedSiteResult = followingManager.GetFollowed(SPSocialActorTypes.Sites); // Get the information from the server. clientContext.ExecuteQuery(); //// Write the results to the console window. //Console.WriteLine("Is the current user following the target document? {0}", isDocFollowed.Value); //Console.WriteLine("Is the current user following the target site? {0}", isSiteFollowed.Value); //if (followedDocCount.Value > 0) //{ // IterateThroughContent(followedDocCount.Value, followedDocResult.Value); //} if (followedSiteCount > 0) { IterateThroughContent(followedSiteCount, followedSiteResult); } Console.ReadKey(); }
protected override void CreateChildControls() { SPSocialFollowingManager followingManager = new SPSocialFollowingManager();//u,serviceContext); SPSocialActorInfo actorInfo = new SPSocialActorInfo(); actorInfo.ContentUri = new Uri(SPContext.Current.Web.Url); actorInfo.ActorType = SPSocialActorType.Site; SPSocialActor[] ifollow = followingManager.GetFollowers(); SPWeb web = SPContext.Current.Web; }
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"; } } } } }); }
private void btnEnum_Click(object sender, EventArgs e) { SPSite site = new SPSite("http://localhost"); try { SPServiceContext serviceContext = SPServiceContext.GetContext(site); UserProfileManager upm = new UserProfileManager(serviceContext); string accountName = "ccc\\" + txtUser.Text.ToString();//xueqingxia";// SPContext.Current.Web.CurrentUser.LoginName; //accountName = accountName.Substring(accountName.LastIndexOf("|") + 1); //if (accountName == "sharepoint\\system") // accountName = "ccc\\xueqingxia"; if (upm.UserExists(accountName)) { UserProfile u = upm.GetUserProfile(accountName); //Response.Write(u[PropertyConstants.PictureUrl].Value); SPSocialFollowingManager follow = new SPSocialFollowingManager(u, serviceContext); SPSocialActorInfo socialInfo = new SPSocialActorInfo(); StringBuilder txt = new StringBuilder(); //des.Text = follow.GetFollowedCount(SPSocialActorTypes.Sites).ToString(); foreach (SPSocialActor spFollow in follow.GetFollowed(SPSocialActorTypes.Sites)) { //spFollow.Status = SPSocialStatusCode.InternalError; socialInfo.ContentUri = spFollow.Uri; //socialInfo.AccountName = u.AccountName; socialInfo.ActorType = SPSocialActorType.Site; //follow.StopFollowing(socialInfo); //des.Text += "ok! "; txt.AppendLine(spFollow.Uri.ToString()); } this.txtResult.Text = txt.ToString(); } } catch (Exception ex) { this.txtResult.Text = ex.ToString(); } }
//"以下代码示例使当前用户开始关注或停止关注某目标项目" 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(); } } } }