Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

How to provision a web analytics web part / whatspopularwebpart programmatically

$
0
0

Hi

I really hope someone can help me crack this nut.

I'm trying to provision a web analytics web part using c# but i can't get it deployed on the page. I create an instance of the webpart and afterwards tries to put it on a page using a limitedwebpartManager.

when the LimitedWebpartManager attempts to add it to the page i get this error :

 There was an error generating the XML document., StackTrace:  
 at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)   
 at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)   
 at Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlAsBytes(XmlSerializer xmls)   
 at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope scope)   
 at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links()   
 at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart, Int32 viewId, String viewGuid)   
 at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)   
 at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)   
 at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)   
 at Code.AddWebPartToPage(WebPart webPart, SPWeb site, String pageUrl, String webpartZone, Int32 zoneIndex, Boolean overwriteWebPartIfExist)

The code can be seen below:

WhatsPopularWebPart webAnalyticWP = new WhatsPopularWebPart()
                {
                    Title = "Populære artikler",
                    DateRange = DateRangeValue.ThreeMonths,
                    SiteScope = SiteScopeType.SiteCollection,
                   };
                webAnalyticWP.SearchScope = "All Sites";
                webAnalyticWP.View = WhatsPopularType.SiteSearchQueries;                
                webAnalyticWP.EnableRSSFeed = false;
    AddWebPartToPage(webAnalyticWP, web, "default.aspx", "RightTopZone", 5, false);
public void AddWebPartToPage(System.Web.UI.WebControls.WebParts.WebPart webPart, SPWeb site, string pageUrl, string webpartZone, int zoneIndex)
        {            
            try
            {
                string pagesBaseUrl = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", site.Language);
                if (!pagesBaseUrl.EndsWith(@"/"))
                    pagesBaseUrl = pagesBaseUrl + @"/";
                pageUrl = pagesBaseUrl + pageUrl;
                SPFile pageToEdit = site.GetFile(pageUrl);
                if (pageToEdit.CheckOutType != SPFile.SPCheckOutType.None)
                {
                    pageToEdit.CheckIn("Forced Check In", SPCheckinType.MinorCheckIn);
                }
                pageToEdit.CheckOut();
                using (SPLimitedWebPartManager webpartCollection = site.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
                {
                    bool SafeUpdates = webpartCollection.Web.AllowUnsafeUpdates;
                    webpartCollection.Web.AllowUnsafeUpdates = true;
                    webpartCollection.AddWebPart(webPart, webpartZone, zoneIndex);
                    webpartCollection.SaveChanges(webPart);
                    webpartCollection.Web.AllowUnsafeUpdates = SafeUpdates;
                    webpartCollection.Web.GetFile(pageUrl).CheckIn("Web parts added");
                    webpartCollection.Web.GetFile(pageUrl).Publish("");
                }
            }
            catch (Exception _e)
            {                
            }
        }

Any help appreciated. The above code works fine on other webparts but not on the webanalytics webpart / whatspopularwebpart


Viewing all articles
Browse latest Browse all 11571

Trending Articles



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