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 DataProvider_BeforeSerializeToClient(object sender, ScriptWebPart.BeforeSerializeToClientEventArgs e)
        {
            DataProviderScriptWebPart dp = sender as DataProviderScriptWebPart;

            if (dp == null)
            {
                return;
            }

            List <string> authorsIFollow = new List <string>();
            List <string> sitesIFollow   = new List <string>();
            SPUser        targetUser     = SPContext.Current.Web.CurrentUser;

            try
            {
                SPServiceContext   serverContext  = SPServiceContext.GetContext(SPContext.Current.Site);
                UserProfileManager profileManager = new UserProfileManager(serverContext);
                if (profileManager.UserExists(targetUser.LoginName))
                {
                    UserProfile profile = profileManager.GetUserProfile(targetUser.LoginName);
                    SPSocialFollowingManager manager       = new SPSocialFollowingManager(profile);
                    SPSocialActor[]          followedUsers = manager.GetFollowed(SPSocialActorTypes.Users);
                    authorsIFollow.AddRange(followedUsers.Select(f => f.Name));

                    SPSocialActor[] followedSites = manager.GetFollowed(SPSocialActorTypes.Sites);
                    sitesIFollow.AddRange(followedSites.Select(f => f.Uri.AbsoluteUri));
                }
            }
            catch (SPSocialException)
            {
                //Silence error if the user don't have a personal site
            }

            if (authorsIFollow.Count > 0)
            {
                dp.Properties["FollowedUsers"] = authorsIFollow;
            }

            if (sitesIFollow.Count > 0)
            {
                dp.Properties["FollowedSites"] = sitesIFollow;
            }
        }
        //以下代码示例获取当前用户正在关注的文档和网站并获取有关用户的关注内容状态的信息
        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();
        }
Beispiel #4
0
        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 int GetSiteFollowedCount()
        {
            string siteUrl = SPContext.Current.Web.Url;
            int    i       = 0;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.Url))
                {
                    SPServiceContext contexto = SPServiceContext.GetContext(currentSite);

                    UserProfileManager perfiles = new UserProfileManager(contexto);

                    foreach (UserProfile userProfile in perfiles)
                    {
                        try
                        {
                            //Check if the user profile is already created
                            if (userProfile.PersonalSiteInstantiationState == PersonalSiteInstantiationState.Created)
                            {
                                SPSocialFollowingManager followingManager = new SPSocialFollowingManager(userProfile, contexto);

                                SPSocialActor[] followedUserSites = followingManager.GetFollowed(SPSocialActorTypes.Sites);

                                SPSocialActor matchSite = followedUserSites.Where(st => st.Uri.AbsoluteUri.Contains(siteUrl)).SingleOrDefault();
                                if (matchSite != null)
                                {
                                    i = i + 1;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            this.Controls.Add(new LiteralControl(ex.ToString()));
                        }
                    }
                }
            });
            return(i);
        }
        //最新的关注的网站和取消的网站
        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";
                            }
                        }
                    }
                }
            });
        }
Beispiel #7
0
        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();
            }
        }
Beispiel #8
0
        void GuanZhuAndStopGuanZhu()
        {
            // Replace the following placeholder values with the URL of the target
            // server and target document (or site).
            //string serverUrl = SPContext.Current.Site.Url;
            string                   contentUrl       = txtDocUrl.Text;
            SPSocialActorType        contentType      = SPSocialActorType.Site;
            SPSocialFollowingManager followingManager = null;

            //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);


            // 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);

            this.Controls.Add(new LiteralControl(" 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)
                {
                    this.Controls.Add(new LiteralControl(" StopFollowing: "));
                    bool isToped = followingManager.StopFollowing(actorInfo);
                    this.Controls.Add(new LiteralControl(isToped.ToString()));
                }
                else
                {
                    //this.Controls.Add(new LiteralControl(" Follow: "));
                    //SPSocialFollowResult result = followingManager.Follow(actorInfo);
                    //// If the result is AlreadyFollowing, then stop following
                    //// the target item.
                    //this.Controls.Add(new LiteralControl("FollowResult:" + result.ToString()));
                }
                SPSocialActor[] ifollow       = followingManager.GetFollowed(SPSocialActorTypes.Sites);
                int             followedCount = followingManager.GetFollowedCount(SPSocialActorTypes.Sites);
                this.Controls.Add(new LiteralControl("GetFollowedCount:" + followedCount.ToString() + "  GetFollowed " + ifollow.Length));
            }
            catch (Exception ex)
            {
                if (SPContext.Current.Web.Url == txtDocUrl.Text)
                {
                    Page.Response.Redirect(SPContext.Current.Web.Url);
                }
                //this.Controls.Add(new LiteralControl(ex.ToString()));
            }
            //}
            //}
        }