Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Programatically know WebPart filename using WebPart object?

$
0
0

Hi all,

Is there a way to know the Filename of a Webpart using a WebPart object?

Below is my code:

//Add Webpart
        public void AddwebPart(SPSite site)
        {
            try
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //Get the page URl
                    string WebURL = web.Url + "/Pages/" + "TestPage.aspx";
                    Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(WebURL, PersonalizationScope.Shared);

                    XmlReader xmlReader = GetWebPartXML(web, "Summary Links.webpart");

                    if (xmlReader != null)
                    {
                        Boolean isExisting = false;
                        foreach (WebPart w in mgr.WebParts)
                        {
    //Check if Webpart is existing
                            if(w.ID.Equals("Summary Links.webpart"))
                                isExisting = true;

                        }

                        if (isExisting == false)
                        {
                            string errMsg = string.Empty;

                            var wp = mgr.ImportWebPart(xmlReader, out errMsg);
                            mgr.AddWebPart(wp, "Zone1", 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
                throw ex;
            }
        }

So that if the webpart I want to add is already existing, I can just not add it anymore.

Thanks!


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>