Freigeben über


SharePoint Check the user is member of audience Programmatically

Audiences in SharePoint Server 2010 are groupings of users that can be used to target content on a SharePoint Server Web site. SharePoint Server 2010 allows targeting to the list-item level or to the list level. Groupings are determined by membership in Microsoft Exchange Server distribution lists, membership in SharePoint groups, or rules that are configured by an administrator. Each audience must be compiled before content can be targeted to that audience. Compilation identifies membership in an audience by crawling the data most recently reported from the identity management system. For more information, see https://technet.microsoft.com/en-gb/library/cc263065(v=office.14).aspx

 

Sometimes we need to identify whether the user is a member of an Audience thru Programmatically. The following code snippet can be used to determine whether the user is part of audience or not. The LogUtility class used in this method just represents the logging technieque but doesn't actually provide the LogUtility class. Use your own logging mechanism.

Example of calling this method : bool isMember = IsUserMemberofAudience("https://SharePointServer/SiteCollection/", "Audience Manager", "domain\loginname");

 

public static bool IsUserMemberofAudience(string siteUrl, string audienceName, string accountName)
        {
            LogUtility.Verbose("IsUserMemberofAudience()", "Entered");
            bool isUserMemberofAudience = false;

            if (string.IsNullOrEmpty(siteUrl) || string.IsNullOrEmpty(audienceName))
            {
                return isUserMemberofAudience;
            }

            SPSecurity.RunWithElevatedPrivileges(
                delegate
                    {
                        SPSite site = default(SPSite);

                        try
                        {
                            using (site = new SPSite(siteUrl))
                            {
                                SPServiceContext serviceContext = SPServiceContext.GetContext(site);

                                if (null != serviceContext)
                                {
                                    var audienceManager = new AudienceManager(serviceContext);
                                    isUserMemberofAudience = audienceManager.IsMemberOfAudience(
                                        accountName, audienceName);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogUtility.Error(
                                "IsUserMemberofAudience()",
                                ex,
                                string.Format(
                                    "An error occurred while checking audience membership for user'{0}' of Audience = '{1}'.",
                                    accountName,
                                    audienceName));
                            isUserMemberofAudience = false;
                            throw;
                        }
                        finally
                        {
                            if (site != null)
                            {
                                site.Dispose();
                            }
                        }
                    });

            return isUserMemberofAudience;
        }