SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have a solution that adds All Assignments Web Part from Sharepoint Learning Kit into a page. Here are code snippets we use to do that:

public static string AddWebPartToPage(SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex)
            using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
                    pageUrl, PersonalizationScope.Shared))
                using (System.Web.UI.WebControls.WebParts.WebPart webPart = CreateWebPart(web, webPartName, webPartManager))

                    webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
                    return webPart.ID;

public static System.Web.UI.WebControls.WebParts.WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager webPartManager)
            SPQuery qry = new SPQuery();
            qry.Query = String.Format(CultureInfo.CurrentCulture, "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>", webPartName);

            SPList webPartGallery = null;

            if (null == web.ParentWeb)
                webPartGallery = web.GetCatalog(SPListTemplateType.WebPartCatalog);
                webPartGallery = web.Site.RootWeb.GetCatalog(SPListTemplateType.WebPartCatalog);

            SPListItemCollection webParts = webPartGallery.GetItems(qry);

            XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());
            string errorMsg;
            System.Web.UI.WebControls.WebParts.WebPart webPart = webPartManager.ImportWebPart(xmlReader, out errorMsg);

            return webPart;

It works ok, but there is the new requirment, which says that this web part should display not only assigments from this site only but from all sites. This web part has custom properties, the one we have to change is called ListScope:

        /// <summary>
        /// Sets the assignment list to display assignments from this site or 
        /// all sites using the same SLK database.
        /// </summary>
        public bool ListScope
            get { return listScope; }
            set { listScope = value; }

How can I change value of this property so web part will be provisioned with new value?

share|improve this question
up vote 1 down vote accepted

It's easy - you need to get your webpart from manager's WebParts collection, cast it to its type, set property and then save changes via manager:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.