Thursday 9 March 2017

Webpart property Updates via a console

using System;
using System.Collections.Generic;

using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace WebpartPropertyConsole
{
    class Program
    {
       
        static void Main(string[] args)
        {
            try
            {
             
                SPSite tempSite = new SPSite("");
                SPWeb tempSPWeb = tempSite.OpenWeb();

                //chartering contract list
               
                //SPList _PGList = tempSPWeb.Lists["Companies"];
                int iTotalItems = _PGList.ItemCount;
                int count = 1;


                int intStartIndex = 106;
                int intEndIndex = 696;

                for (int i = intStartIndex; i <= intEndIndex; i++)
                {
                    //if (i != 2416)
                    {
                        try
                        {
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                try
                                {
                                    SPListItem spli = _PGList.GetItemById(i);
                                    if (spli["Link"] != null)
                                    {
                                        SPFieldUrlValue spfuv = new SPFieldUrlValue(spli["Link"].ToString());
                                        //AddWebpartProperty(spfuv.Url, "Sub Projects", "MainListFilterField", "Link to main project", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Sub Projects", "PortfolioField", "Main project", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Ongoing Valve Delivery Process", "SourceFields", "IQS unique ID,Project name,Link,Project Type,Department,End user", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "All Valve Delivery Process", "SourceFields", "IQS unique ID,Project name,Link,Project Type,Department,End user", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Parts", "ParentListSerialNoField", "Serial No", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Parts", "EquOrPartNo", "Part no", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Parts", "SerialNo", "Part Serial no", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Tools", "PartInventory", "Equipment inventory", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Tools", "PartInventoryFieldPartNo", "Equipment no", spli["ID"].ToString(), _PGList.ID.ToString());
                                        AddWebpartProperty(spfuv.Url, "Chartering Contract | Details", "LinkStyle", "font-family:verdana;color:#002B54;padding-top:16px;padding-left:16px;font-size:15px;font-weight:bold;", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Parts", "StatusFieldValue", "", spli["ID"].ToString(), _PGList.ID.ToString());

                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Tools", "Hidenewserialno", "NO", spli["ID"].ToString(), _PGList.ID.ToString());
                                        //AddWebpartProperty(spfuv.Url, "Assign Serial Number for Tools", "Equipmentserialnoguidformat", "NO", spli["ID"].ToString(), _PGList.ID.ToString());
                                        Console.WriteLine(count + "/" + iTotalItems);
                                        count++;

                                    }
                                    else
                                    {

                                    }
                                }
                                catch
                                {



                                }
                            });
                        }

                        catch
                        {
                            ;
                        }
                    }

                }

            }
            catch
            {
                ;
            }

        }
     

        #region Add Property
        /// <summary>
        ///
        /// </summary>
        /// <param name="weburl"></param>
        /// <param name="webPartName"></param>
        /// <param name="webpartProprty"> property that is set in webpart code (note: not display name)</param>
        /// <param name="webpartPropertyValue"></param>
        /// <param name="id"></param>
        /// <param name="strlist"></param>
        /// <param name="spWeb1Parent"></param>

        public static void AddWebpartProperty(string weburl, string webPartName, string webpartProprty, string webpartPropertyValue, string id, string strlist)
        {

            //string strTargetSite = "http://ccsentshivm058/shibu/default.aspx";
            string strTargetSite = weburl;
            SPSite spsSite = new SPSite(strTargetSite);
            SPWeb spwWeb = spsSite.OpenWeb();

            string stringrelativeurl = spwWeb.ServerRelativeUrl;
            spwWeb.AllowUnsafeUpdates = true;

            //const string KEY_CHK = "DWIndivid_weburl";

            //if (!spwWeb.Properties.ContainsKey(KEY_CHK))
            //{
            //    spwWeb.Properties.Add("DWIndivid_weburl", spWeb1Parent.Url);  // create web url
            //}
            //else
            //{
            //    spwWeb.Properties.Remove("DWIndivid_weburl");  // create web url
            //    spwWeb.Properties.Add("DWIndivid_weburl", spWeb1Parent.Url);
            //}

            //if (!spwWeb.Properties.ContainsKey("DWIndivid_RelativeWebUrl"))
            //{
            //    spwWeb.Properties.Add("DWIndivid_RelativeWebUrl", spWeb1Parent.ServerRelativeUrl); //relatiev url

            //}
            //else
            //{
            //    spwWeb.Properties.Remove("DWIndivid_RelativeWebUrl");
            //    spwWeb.Properties.Add("DWIndivid_RelativeWebUrl", spWeb1Parent.ServerRelativeUrl);
            //}
            //if (!spwWeb.Properties.ContainsKey("DWIndivid_ListID"))
            //{
            //    spwWeb.Properties.Add("DWIndivid_ListID", strlist); //relatiev url
            //}
            //else
            //{
            //    spwWeb.Properties.Remove("DWIndivid_ListID");
            //    spwWeb.Properties.Add("DWIndivid_ListID", strlist);
            //}
            //if (!spwWeb.Properties.ContainsKey("DWIndivid_ItemID"))
            //{
            //    spwWeb.Properties.Add("DWIndivid_ItemID", id); //relatiev url
            //}
            //else
            //{
            //    spwWeb.Properties.Remove("DWIndivid_ItemID");
            //    spwWeb.Properties.Add("DWIndivid_ItemID", id);
            //}

            //spwWeb.Properties.Update();//save site property


            // checking webpart property is having ',' at the end
            //checking webpart propertyvalue is having ','at the end

            string str = webpartProprty.Substring(webpartProprty.Length - 1);
            if (str == ",")
            {


            }
            else
            {
                webpartProprty = webpartProprty + ",";
            }
            string strpro = webpartPropertyValue.Substring(webpartPropertyValue.Length );
            if (strpro == ",")
            {

            }
            else
            {
                webpartPropertyValue = webpartPropertyValue + ",";
            }

            //Delivery%20Ticket%20Report.aspx
            //SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection("default.aspx", Storage.Shared);

            //SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection("/webpartpages/bussprocess.aspx", Storage.Shared);
            //WorkOrderOutReport.aspx
            //    string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/" + "webpartpages/bussprocess.aspx";
            //string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/WebPage/OngoingProjects.aspx"; //+ "/" + "webpartpages/Quotation And Invoice Webpart.aspx";
            // string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/default.aspx"; //+ "/" + "webpartpages/Quotation And Invoice Webpart.aspx";
            //string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/webpartpages/Serial No Allocation WebPart For Part.aspx"; //+ "/" + "webpartpages/Quotation And Invoice Webpart.aspx";
            string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/default.aspx"; //+ "/" + "webpartpages/Quotation And Invoice Webpart.aspx";

            //Serial%20No%20AllocationWebPart.aspx
            // string strRelativeUrl = spwWeb.ServerRelativeUrl.ToString() + "/" + "webpartpages/busprocess.aspx";
            SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection(strRelativeUrl, Storage.Shared);

            //http://nos.signin.no/DivAB-12-004587/webpartpages/Delivery%20Ticket%20Report.aspx

            foreach (Microsoft.SharePoint.WebPartPages.WebPart wptWebPart in wpcWebParts) //going through each webpart
            {
                //Console.WriteLine(wptWebPart.Title.ToString());
                //if (wptWebPart.Title == webPartName.ToString()) //comparing webpart name with title
                string str3 = wptWebPart.Title;
                if (wptWebPart.Title == webPartName)
                {
                    PropertyInfo[] pinProperties = wptWebPart.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo pinProperty in pinProperties)
                    {
                        string[] prpname = webpartProprty.Split(','); //Taking webpart property name;
                        string[] val = webpartPropertyValue.Split(',');//Taking webpart property value
                        string strwebpartPropertyValue = webpartPropertyValue.ToString();
                        strwebpartPropertyValue = strwebpartPropertyValue.Remove(strwebpartPropertyValue.Length - 1);

                        int i = 0;

                        foreach (string word in prpname)
                        {
                            //Console.WriteLine(strwebpartPropertyValue.ToString());
                            if (pinProperty.Name == word)
                            {
                                // Console.WriteLine(pinProperty.Name.ToString());

                                //pinProperties.SetValue(wptWebPart,

                                // bool bValue = true;                              
                                //   pinProperty.SetValue(wptWebPart, bValue, null); // setting true or false


                                //pinProperty.SetValue(wptWebPart, val[i], null); //setting  webpart  value
                               
                                pinProperty.SetValue(wptWebPart, strwebpartPropertyValue, null);
                                wpcWebParts.SaveChanges(wptWebPart.StorageKey);//saving changes

                            }
                            i++;
                        }


                    }
                    //if (webPartName == "Ongoing Valve Delivery Process")
                    //{
                    //    wptWebPart.Title = "Ongoing Business Process";
                    //}
                    //if (webPartName == "All Valve Delivery Process")
                    //{
                    //    wptWebPart.Title = "All Business Process";
                    //}
                    //wpcWebParts.SaveChanges(wptWebPart.StorageKey);

                    break;
                }
            }


            spwWeb.AllowUnsafeUpdates = false;
            spwWeb.Dispose();
            spsSite.Dispose();
            //Console.ReadLine();

        }


        #endregion
    }
}

No comments:

Post a Comment