Responsive Ads Here

Tuesday, July 17, 2012

Set target audience to Quick Launch item programmatically


SPSecurity.RunWithElevatedPrivileges(delegate()
               {
                   using (SPSite spSite = new SPSite(siteUrl))
                   {
                       using (SPWeb spWeb = spSite.OpenWeb())
                       {                           
                           //First delete all the items in quick launch
                           for (int i = spWeb.Navigation.QuickLaunch.Count - 1; i > -1; i--)
                           {
                               if (spWeb.Navigation.QuickLaunch[i].Title == "Admin Panel")
                                spWeb.Navigation.QuickLaunch[i].Delete();                            
                           }
                           spWeb.Update();
                           //Add items to Quick Launch

                           //Quick Launch item - Meeting Work Space
                           //SPList lst = spWeb.Lists[Constants.LstContacts];//"Contacts"
                           SPNavigationNodeCollection nodes = spWeb.Navigation.QuickLaunch;
                           string TargetGroups = "2-Project Admin";
                           SPNavigationNode navNode2 = newSPNavigationNode("Admin Panel","/sites/Projects/BHEL/Pages/adminpanel.aspx",true);
                           nodes.AddAsLast(navNode2);
                           if (navNode2.Properties.Contains("Audience"))
                           {
                               //// Set Property
                               navNode2.Properties["Audience"] = TargetGroups;
                           }
                           else
                           {
                               //// Add Property
                               navNode2.Properties.Add("Audience", TargetGroups);
                           }
                           navNode2.Update();
                          
                           spWeb.Update();
                       }
                   }
               });

No comments:

Post a Comment