Thursday 9 March 2017

Hide Webpart and Remove it from Quick Launch

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web;
using System.Xml;
using System.Web.UI;
using Microsoft.SharePoint.Navigation;
namespace Solstad_HIdeWebpartandQuickLaunch
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            string ListName = "ListNAme";

            Int32 intStartItmID = 106;
            Int32 intEndItmID = 648;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite("SiteName"))
                {

                    SPWeb web = site.OpenWeb();
                    SPList spList = web.Lists[ListName];
                    SPQuery spQItems = new SPQuery();
                    spQItems.Query = "<Where>   <And>  <Geq>  <FieldRef Name='ID' /><Value Type='Counter'>" + intStartItmID + "</Value> " +
                                   "  </Geq>         <Leq><FieldRef Name='ID' />     <Value Type='Counter'>" + intEndItmID + "</Value>   " +
                                   "  </Leq>               </And>       </Where>";
                    SPListItemCollection items = spList.GetItems(spQItems);
                    if (items.Count > 0)
                    {
                        i = items.Count;
                        foreach (SPListItem item in items)
                        {
                            //string strPropjectType = Convert.ToString(item["Project_x0020_type_x002e_"]);
                            //if (strPropjectType == "General Project")
                            //{
                            string strLink = Convert.ToString(item["Link"]);
                         
                                if (strLink != "")
                                {
                                    try
                                    {
                                        SPFieldUrlValue spfuv = new SPFieldUrlValue(strLink.ToString());

                                        SPSite SubSite = new SPSite(spfuv.Url);
                                        SPWeb subWeb = SubSite.OpenWeb();

                                        /*
                                        Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;
                                        mgr = subWeb.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                                        foreach (System.Web.UI.WebControls.WebParts.WebPart myWebPart in mgr.WebParts)
                                        {
                                            if (myWebPart.Title == "IQS_BusinessProcess_ProcessesViewer")
                                            {
                                                myWebPart.Hidden = false;
                                                mgr.SaveChanges(myWebPart);
                                                break;
                                            }
                                            //Business Process

                                        }
                                         */
                                     
                                        int j;
                                        SPNavigationNodeCollection quickLaunchNodes = subWeb.Navigation.QuickLaunch;
                                        foreach (SPNavigationNode node in quickLaunchNodes)
                                        {
                                            //for (int k = 0; k < quickLaunchNodes[0].Children.Count; k++)
                                            //{
                                            SPNavigationNode nn;
                                            try
                                            {
                                                nn = quickLaunchNodes.Navigation.GetNodeById(quickLaunchNodes[0].Children[0].Id);
                                                if (nn.Title.Equals("Contract Life Cycle Process"))
                                                {
                                                    nn.Delete();
                                                }
                                               
                                            }
                                            catch { }
                                            try
                                            {
                                                nn = quickLaunchNodes.Navigation.GetNodeById(quickLaunchNodes[0].Children[1].Id);
                                                if (nn.Title.Equals("Contract Life Cycle Process"))
                                                {
                                                    nn.Delete();
                                                }
                                            }
                                            catch { }
                                            //nn = nodesDemo.Navigation.GetNodeById(nodesDemo[i].Children[j].Id);
                                            //if (node.Title.Equals("QCP Process"))
                                            //{;
                                            //    node.Delete();
                                            //}
                                            //}
                                        }
                                        i = i - 1;
                                       
                                        Console.WriteLine("ID" + Convert.ToString(item["ID"]));
                                        //Console.ReadLine();
                                        //for (j = subWeb.Navigation.QuickLaunch.Count - 1; j > 0; j--)
                                        //{

                                        //    //subWeb.Navigation.QuickLaunch.Delete(subWeb.Navigation.QuickLaunch[i]);
                                        //}
                                    }
                                    catch { }
                                }
                           
                            //}
                           
                        }
                    }
               

                }
            });
        }
     
    }
}

No comments:

Post a Comment