SharePoint, there are two ways to upgrade the Web Part properties: AfterDeserialize override a method, and implement the user interface IversioningPersonalizable. In this article, we assume that our project is called MyWebpart, and is a feature called the Source list. Source list to save the origin of the name of the Web Part to connect to information, and its value is a string (such as SharePoint If you connect to a SharePoint list, and if you are connected to SQL for SQL Server:
Now I need to upgrade to version 2.0, and I need to replace the old properties with a new property named DataSource.
1. Implementing AfterDeserialize:
I implement an override using the AfterDeserialize method, which will go through and access each of property of the Web Part in version 1.0. Thus, in this method, I have a loop statement to get the value for each of the elements, then assign them to the new property in version 2.0. In the case of the image belowk, I will get the value of ListSource and assign to DataSource property.
Note: Each XmlElement element in the UnknownXmlElements properties is a Web Part property.
After assigning the new property, I need to remove the old property from UnknowXmlElement so that next time, this property will not upgrade again. This prevents duplicate data, or setting the wrong value whenever the method executes.
2. Implementing IVersioningPersonalizable:
In this solution, I need to inherit the interface named IVersioningPersonalizable, which is supported by SharePoint, and declare a variable named oldProperties to store all properties of the old version of the Web Part. To get the old properties of the previous version of the Web Part, implement the code in the Load method of this interface as shown below:
The Method Load of IVersioningPersonalize is executed first, so I need to upgrade the properties in OnInit of the Web Part. I implement a loop statement to go through all properties as with the above solution. To avoid the duplicate or updating the wrong value whenever OnInit is executed, after upgrading the properties, I set the oldProperties variable to be null, indicating that we have already upgraded.
Be aware that, when upgrading Web Parts for Publishing sites, you will likely get an error at this.SaveProperties = true. If you see this error, it means you'll need to check out your publishing site before upgrading the Web Part.