Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
            }
            else
            {
                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>
        [WebBrowsable(),
         AlwpWebDisplayName("AlwpListScopeDisplayName"),
         AlwpWebDescription("AlwpListScopeDescription")]
        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
add comment

1 Answer

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:

manager.SaveChanges(webPart);
share|improve this answer
add comment

Your Answer

 
discard

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.