Monday, 2 April 2012

Set Target Audience for an visual webpart programmatically


 

public void webpartfilter()

{

try

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite osite = new SPSite(SPContext.Current.Site.ID))

  using (SPWeb oweb = osite.OpenWeb())

   {

     SPLimitedWebPartManager mgr = null;

     mgr = oweb.GetLimitedWebPartManage("/SitePages/Totus.aspx",PersonalizationScope.Shared);

     foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in mgr.WebParts)

    {

     if (webPart.Title == "FacultyNotes")

     {

      SPGroupCollection ogrpcoll = oweb.Groups;

      foreach (SPGroup ogrp in ogrpcoll)

      {

     if (ogrp.Name.Contains("Faculty") || ogrp.Name.Contains("faculty"))

     {

     string audWebPart = webPart.AuthorizationFilter;

     if (audWebPart == "")

    {

    webPart.AuthorizationFilter = ";;;;" + ogrp.Name;

    }

    else

    {

    if (!webPart.AuthorizationFilter.Contains(ogrp.Name))

   webPart.AuthorizationFilter = audWebPart + "," + ogrp.Name;

}

   mgr.SaveChanges(webPart);

   }

  }

 }

 }

 }

 }

 });

}

 catch (Exception ex)

 {

  throw ex;

  }

 }

 }

 }

                                       Set Target Audience For An Visual webpart                                                 
ds

{
public void webpartfilter()try{

{

{

{

mgr = oweb.GetLimitedWebPartManager(

{

{


{

{


{
webPart.AuthorizationFilter =
}
SPSecurity.RunWithElevatedPrivileges(delegate()using (SPSite osite = new SPSite(SPContext.Current.Site.ID))using (SPWeb oweb = osite.OpenWeb())SPLimitedWebPartManager mgr = null;"/SitePages/Totus.aspx",PersonalizationScope.Shared);foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in mgr.WebParts)if (webPart.Title == "FacultyNotes")SPGroupCollection ogrpcoll = oweb.Groups;foreach (SPGroup ogrp in ogrpcoll)if (ogrp.Name.Contains("Faculty") || ogrp.Name.Contains("faculty"))string audWebPart = webPart.AuthorizationFilter;if (audWebPart == "")";;;;" + ogrp.Name;else{

webPart.AuthorizationFilter = audWebPart +
}
mgr.SaveChanges(webPart);
}
}
}
}
}
}
});
}

{

}
}
if (!webPart.AuthorizationFilter.Contains(ogrp.Name))"," + ogrp.Name;catch (Exception ex)throw ex;