SharePoint Tips

Thursday, November 16, 2017

C# - SharePoint - Get Current User Audience Group

public static string GetCurrentUserAudienceGroup(string publishingSiteURL, string loginName)
 {
AcenetLogger.LogMessage("Entering into the Method GetCurrentUserAudienceGroup", "CommonHelper", null);
string audienceGroups = string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(publishingSiteURL))
{
SPWeb web = site.RootWeb;

Microsoft.Office.Server.ServerContext context = Microsoft.Office.Server.ServerContext.GetContext(site);
AudienceManager audManager = new AudienceManager(context);
AudienceCollection audiences = audManager.Audiences;

for (int i = 0; i < audiences.Count; i++)
{
if (audiences[i].IsMember(loginName))
{
audienceGroups = audienceGroups + "," + audiences[i].AudienceID;
}
}

}
});
}
catch (Exception ex)
{
Logger.LogException("Error has occured On the Method”);
audienceGroups = string.Empty;
}
return audienceGroups.TrimStart(',');
 }

No comments:

Post a Comment