0

I want to set a custom property value when add web part programmatically.

This is my code for add web part in page:

            using (SPLimitedWebPartManager manager = oWeb.GetLimitedWebPartManager(newPage.Url, PersonalizationScope.Shared))
            {
                try
                {
                    string wPartURL = oWeb.Site.Url + "/_catalogs/wp/ClientViewProjects.webpart";
                    XmlTextReader reader = new XmlTextReader(new StringReader(oWeb.GetFileAsString(wPartURL)));

                    string err = null;
                    System.Web.UI.WebControls.WebParts.WebPart wp = (System.Web.UI.WebControls.WebParts.WebPart)manager.ImportWebPart(reader, out err);
                    wp.ChromeType = PartChromeType.None;
                    manager.AddWebPart(wp, "Header", 0); //See the description below for how to determine Zone
                    manager.SaveChanges(wp);
                }
                catch (Exception ex)
                {
                    LOG_STRING += string.Format("В сайт с URL:{0}{1} {2}", oWeb.ServerRelativeUrl, ex.ToString(), Environment.NewLine);
                }
                finally
                {
                    manager.Web.Dispose();
                }

                newPage.CheckIn("Checked in by Code");
                newPage.ListItem.File.Publish("Publishing major version");
            }
3
  • "wp.CustomPropertName = value" isn't working? Commented Oct 16, 2019 at 8:12
  • no such property
    – Goshky
    Commented Oct 16, 2019 at 8:26
  • Please add the code where you have create custom property Commented Oct 16, 2019 at 8:44

1 Answer 1

0

This is the answer:

PropertyInfo[] pinProperties = wp.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
     foreach (PropertyInfo pinProperty in pinProperties)
     {
     if (pinProperty.Name == "City")
     {
            pinProperty.SetValue(wp, "propValue", null);
     }
     if (pinProperty.Name == "Status")
     {
           pinProperty.SetValue(wp, "propValue", null);
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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