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");
            }
  • "wp.CustomPropertName = value" isn't working? – harshal gite Oct 16 at 8:12
  • no such property – Goshky Oct 16 at 8:26
  • Please add the code where you have create custom property – harshal gite Oct 16 at 8:44
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, privacy policy and cookie policy

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