Second Life of a Hungarian SharePoint Geek

October 17, 2010

Getting the type of the web part from a BinarySerializedWebPart

As I promised earlier, in this post I will show you a simple way to determine the type of the web part that is encoded in a BinarySerializedWebPart. The code in this post is only an extension to the code example in my former post.

The WebPart node of the BinarySerializedWebPart XML contains an attribute called WPTypeId that has a GUID value.

The internal WebPartTypeInfo class (Microsoft.SharePoint.WebPartPages namespace, Microsoft.SharePoint assembly) contains a static method, TryGetWellKnownTypeId, that provides the Type of the well known (built-in) web parts as an out parameter based on this GUID value.

The code below shows how to access this information using Reflection:

  1. XmlAttribute attribute = webPartNode.Attributes["WPTypeId"];
  2. if (attribute != null)
  3. {
  4.     Type webPartTypeInfoType = assembly.GetType("Microsoft.SharePoint.WebPartPages.WebPartTypeInfo");
  5.  
  6.     Guid wpTypeId = new Guid(attribute.Value);
  7.  
  8.     //internal static bool TryGetWellKnownTypeId(Guid webPartTypeId, out Type type);
  9.     MethodInfo mi_TryGetWellKnownTypeId = webPartTypeInfoType.GetMethod("TryGetWellKnownTypeId", BindingFlags.NonPublic | BindingFlags.Static);
  10.  
  11.     Type webPartType = null;
  12.  
  13.     object[] parameters = new object[2] { wpTypeId, webPartType };
  14.  
  15.     if ((bool)mi_TryGetWellKnownTypeId.Invoke(null, parameters))
  16.     {
  17.         webPartType = (Type)parameters[1];
  18.         Console.WriteLine("Web part type is: {0}", webPartType);
  19.     }
  20. }

You can use the result as the type parameter when calling the constructor of the BinaryWebPartDeserializer.

Advertisements

Decoding the content of the BinarySerializedWebPart – The code

Note: this is the second part of a former post. If you happen to have questions, please check the first part (that is about the theory) before asking something you may find the answer for in the first part.

Well, after so much theory I feel it is time to write some code for decoding of BinarySerializedWebPart.

Let’s see my former sample XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <Module Name="FileSystemForms" Url="Lists/Files" RootWebOnly="FALSE" SetupPath="pages">
  4.     <File Url="DispForm.aspx" Type="Ghostable" Path="form.aspx">
  5.       <BinarySerializedWebPart>
  6.         <GUIDMap>
  7.           <GUID Id="33ff2881_489d_4ce2_ac94_e81d64689d2a" ListUrl="Lists/Files" />
  8.         </GUIDMap>
  9.         <WebPart ID="{035cec7d-5f69-4dbf-a551-0b8203467c41}" WebPartIdProperty="" List="{$ListId:Lists/Files;}" Type="4"
  10.           Flags="0" DisplayName="" Version="4" Url="Lists/Files/DispForm.aspx" WebPartOrder="1" WebPartZoneID="Main"
  11.           IsIncluded="True" FrameState="0" WPTypeId="{feaafd58-2dc9-e199-be37-d6cdd7f84690}"
  12.           SolutionId="{00000000-0000-0000-0000-000000000000}" Assembly="" Class="" Src=""
  13.           AllUsers="B6Dt/kMAAAABAAAAAAAAAAIAAAAvX2xheW91dHMvaW1hZ2VzL2l0ZWJsLnBuZwAvRjFTaXRlL0xpc3RzL0ZpbGVzAP8BFCsAJQICAgMCAwEEAAICAhICFAEBAAIEBQtDb250cm9sTW9kZQspiAFNaWNyb3NvZnQuU2hhcmVQb2ludC5XZWJDb250cm9scy5TUENvbnRyb2xNb2RlLCBNaWNyb3NvZnQuU2hhcmVQb2ludCwgVmVyc2lvbj0xNC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj03MWU5YmNlMTExZTk0MjljAQUIRm9ybVR5cGUCBAEAAAIWAoYBCyo0U3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5XZWJQYXJ0cy5XZWJQYXJ0RXhwb3J0TW9kZQICggEFGi9fbGF5b3V0cy9pbWFnZXMvaXRlYmwucG5nAn0FEy9GMVNpdGUvTGlzdHMvRmlsZXMFCFBhZ2VUeXBlCyl3TWljcm9zb2Z0LlNoYXJlUG9pbnQuUEFHRVRZUEUsIE1pY3Jvc29mdC5TaGFyZVBvaW50LCBWZXJzaW9uPTE0LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTcxZTliY2UxMTFlOTQyOWMEBQdMaXN0VXJsZQUGT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hbHVlPSJDdXJyZW50VXNlck5hbWUiLz4NCiAgICAgICAgPFBhcmFtZXRlckJpbmRpbmcgTmFtZT0iVG9kYXkiIExvY2F0aW9uPSJDQU1MVmFyaWFibGUiIERlZmF1bHRWYWx1ZT0iQ3VycmVudERhdGUiLz4NCiAgICAgICAgPFBhcmFtZXRlckJpbmRpbmcgTmFtZT0iTGlzdEl0ZW1JZCIgTG9jYXRpb249IlF1ZXJ5U3RyaW5nKElEKSIgRGVmYXVsdFZhbHVlPSIwIi8+DQogICAgICAgIA==" />
  14.       </BinarySerializedWebPart>
  15.       </File>
  16.     </Module>
  17. </Elements>

The DeserializeWebPart method below shows the decoding process of the encoded web part. It first read and Base64 decode the binary serialized attribute value, then instantiate the BinaryWebPartDeserializer via calling its protected constructor, and gets the web part by calling the internal Deserialize method. Note, that we get the SPWebPartManager we need also using Reflection, by reading the internal WebPartManager property of the SPWeb instance passed to the method as parameter. I assume you can use any SPWeb instance here, but it requires yet further testing.

In the XML in this example, there is only an AllUsers attribute. If your XML contains PerUser and/or View attributes, you should read those values too, and include them as parameters when calling the constructor of the BinaryWebPartDeserializer.

In this example I knew that the result must be an XsltListViewWebPart, so I used that fixed type in the code. If you are not sure in the web part type, I will show you a method in a following post how to get the type from the XML either.

  1. public WebPart DeserializeWebPart(SPWeb web, String xmlPath)
  2. {
  3.     
  4.     XmlDocument sampleXml = new XmlDocument();
  5.     sampleXml.Load(xmlPath);
  6.     XmlNamespaceManager nsmgr = new XmlNamespaceManager(sampleXml.NameTable);
  7.     nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/&quot;);
  8.     XmlNode webPartNode = sampleXml.SelectSingleNode("sp:Elements/sp:Module/sp:File/sp:BinarySerializedWebPart/sp:WebPart", nsmgr);
  9.  
  10.     byte[] allUsers = GetBinarySerializedAttribute("AllUsers", webPartNode);
  11.     //byte[] perUser = GetBinarySerializedAttribute("PerUser", webPartNode);
  12.     //byte[] view = GetBinarySerializedAttribute("View", webPartNode);
  13.     
  14.     Assembly assembly = typeof(SPSite).Assembly;
  15.     
  16.     Type spWebPartManagerType = typeof(SPWebPartManager);
  17.     Type xmlNamespaceManagerType = typeof(XmlNamespaceManager);
  18.     Type spWebType = typeof(SPWeb);
  19.     Type typeType = typeof(Type);
  20.     Type stringArrayType = typeof(String).MakeArrayType();
  21.     Type byteArrayType = typeof(byte).MakeArrayType();
  22.     // or alternatively we could use
  23.     //Type byteArrayType = typeof(byte[]);
  24.     Type stringType = typeof(String);
  25.     Type xsltListViewWebPartType = typeof(XsltListViewWebPart);
  26.  
  27.     PropertyInfo pi_WebPartManager = spWebType.GetProperty("WebPartManager", BindingFlags.NonPublic | BindingFlags.Instance);
  28.     SPWebPartManager spWebPartManager = (SPWebPartManager)pi_WebPartManager.GetValue(web, null);
  29.  
  30.     Type binaryWebPartDeserializerType = assembly.GetType("Microsoft.SharePoint.WebPartPages.BinaryWebPartDeserializer");
  31.     // protected BinaryWebPartDeserializer(SPWebPartManager webPartManager, XmlNamespaceManager xmlnsManager, byte[] userData, byte[] sharedData, string webPartIdProperty, string[] links, Type type, SPWeb spWeb) : base(webPartManager)
  32.     ConstructorInfo ci_BinaryWebPartDeserializerType = binaryWebPartDeserializerType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
  33.          null, new Type[8] { spWebPartManagerType, xmlNamespaceManagerType, byteArrayType, byteArrayType, stringType, stringArrayType, typeType, spWebType }, null);
  34.     object binaryWebPartDeserializer = ci_BinaryWebPartDeserializerType.Invoke(new object[8] { null, null, null, allUsers, null, null, xsltListViewWebPartType, null });
  35.     MethodInfo mi_Deserialize = binaryWebPartDeserializerType.GetMethod("Deserialize", BindingFlags.NonPublic | BindingFlags.Instance);
  36.     WebPart webPart = (WebPart)mi_Deserialize.Invoke(binaryWebPartDeserializer, null);
  37.  
  38.     return webPart;
  39. }

Note, that the BinarySerializedWebPart node might be found in other context either. See the following example:

  1. <View List="Shared Documents" DisplayName="" Url="" DefaultView="FALSE" BaseViewID="1" Type="HTML" WebPartOrder="0" WebPartZoneID="Left" ContentTypeID="0x" ID="g_ba709f71_6af5_4e3c_a8b1_01be2d3f95e8" Hidden="TRUE">
  2.   <BinarySerializedWebPart>
  3.     <GUIDMap>
  4.       <GUID Id="33ff2881_489d_4ce2_ac94_e81d64689d2a" ListUrl="Lists/Files" />
  5.     </GUIDMap>
  6.     <WebPart ID="{035cec7d-5f69-4dbf-a551-0b8203467c41}" WebPartIdProperty="" List="{$ListId:Lists/Files;}" Type="4"
  7.              Flags="0" DisplayName="" Version="4" Url="Lists/Files/DispForm.aspx" WebPartOrder="1" WebPartZoneID="Main"
  8.              IsIncluded="True" FrameState="0" WPTypeId="{feaafd58-2dc9-e199-be37-d6cdd7f84690}"
  9.              SolutionId="{00000000-0000-0000-0000-000000000000}" Assembly="" Class="" Src=""
  10.              AllUsers="B6Dt/kMAAAABAAAAAAAAAAIAAAAvX2xheW91dHMvaW1hZ2VzL2l0ZWJsLnBuZwAvRjFTaXRlL0xpc3RzL0ZpbGVzAP8BFCsAJQICAgMCAwEEAAICAhICFAEBAAIEBQtDb250cm9sTW9kZQspiAFNaWNyb3NvZnQuU2hhcmVQb2ludC5XZWJDb250cm9scy5TUENvbnRyb2xNb2RlLCBNaWNyb3NvZnQuU2hhcmVQb2ludCwgVmVyc2lvbj0xNC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj03MWU5YmNlMTExZTk0MjljAQUIRm9ybVR5cGUCBAEAAAIWAoYBCyo0U3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5XZWJQYXJ0cy5XZWJQYXJ0RXhwb3J0TW9kZQICggEFGi9fbGF5b3V0cy9pbWFnZXMvaXRlYmwucG5nAn0FEy9GMVNpdGUvTGlzdHMvRmlsZXMFCFBhZ2VUeXBlCyl3TWljcm9zb2Z0LlNoYXJlUG9pbnQuUEFHRVRZUEUsIE1pY3Jvc29mdC5TaGFyZVBvaW50LCBWZXJzaW9uPTE0LjAuMC4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QYXJhbWV0ZXJCaW5kaW5nIE5hbWU9IlVzZXJJRCIgTG9jYXRpb249IkNBTUxWYXJpYWJsZSIgRGVmYXVsdFZhbHVlPSJDdXJyZW50VXNlck5hbWUiLz4NCiAgICAgICAgPFBhcmFtZXRlckJpbmRpbmcgTmFtZT0iVG9kYXkiIExvY2F0aW9uPSJDQU1MVmFyaWFibGUiIERlZmF1bHRWYWx1ZT0iQ3VycmVudERhdGUiLz4NCiAgICAgICAgPFBhcmFtZXRlckJpbmRpbmcgTmFtZT0iTGlzdEl0ZW1JZCIgTG9jYXRpb249IlF1ZXJ5U3RyaW5nKElEKSIgRGVmYXVsdFZhbHVlPSIwIi8+DQogICAgICAgIA==" />
  11.   </BinarySerializedWebPart>
  12. </View>

For this type of XML, you can get the node using the code:

  1. XmlDocument sampleXml = new XmlDocument();
  2. sampleXml.Load(xmlPath);
  3. XmlNode webPartNode = sampleXml.SelectSingleNode("View/BinarySerializedWebPart/WebPart");

No let’s see the helper methods I used. First there is the GetBinarySerializedAttribute method that reads and Base64 decodes the attribute value. It returns only the byte array that we can be used later in the Deserialize method of the ObjectStateFormatter class. So we have to look for the leading xFF x01 byte pattern in the decoded byte array. In this case I chose not the most sophisticated way of search: I simple convert the byte array to a hexadecimal representation and look for the position of the “-FF-01” substring. Since we have not to work with large streams and byte pattern, this algorithm is good / quick enough and provides hopefully more readable code than an arbitrary byte pattern finder implementation.

  1. private byte[] GetBinarySerializedAttribute(String attributeName, XmlNode webPartNode)
  2. {
  3.     byte[] decodedBytes = null;
  4.     XmlAttribute attribute = webPartNode.Attributes[attributeName];
  5.     if (attribute != null)
  6.     {
  7.         String serializedValue = attribute.Value;
  8.         decodedBytes = Convert.FromBase64String(serializedValue);
  9.     }
  10.  
  11.     String fullHexString = ByteArrayToString(decodedBytes);
  12.     int startPos = fullHexString.IndexOf("-FF-01");
  13.     String contentHexString = fullHexString.Substring(startPos);
  14.     byte[] result = StringToByteArray(contentHexString);
  15.  
  16.  
  17.     // this few lines of code only to test the result of deserialization
  18.     // you can remove it
  19.     ObjectStateFormatter formatter = new ObjectStateFormatter();
  20.     if ((result != null) && (result.Length != 0))
  21.     {
  22.         Object deserialized = formatter.Deserialize(new MemoryStream(result));
  23.     }
  24.     // end of testing block
  25.  
  26.     return result;
  27. }

Part of this method is only to test what type of object the deserialization will result. In my experience it is a simple object array as shown here:

image

Further helper methods for byte array – string conversions:

  1. public byte[] StringToByteArray(String hex)
  2. {
  3.     int NumberChars = hex.Length;
  4.     byte[] bytes = new byte[NumberChars / 3];
  5.     for (int i = 0; i < NumberChars; i += 3)
  6.         bytes[i / 3] = Convert.ToByte(hex.Substring(i + 1, 2), 16);
  7.     return bytes;
  8. }
  9.  
  10. private string ByteArrayToString(byte[] ba)
  11. {
  12.     string hex = BitConverter.ToString(ba);
  13.     // append a leading – to the hex string to ensure each byte is 3 chars
  14.     return "-" + hex;
  15. }

Hopefully the code runs successfully and return the deserialized WebPart. From this object you can get the values you need either writing out the necessary properties or simply via checking the properties in Visual Studio IDE.

image

Decoding the content of the BinarySerializedWebPart – The theory

When you save your SharePoint site as a template, the files within the generated WSP package contain the customized web parts as BinarySerializedWebPart, a format not easy to work with (see an example in my former post: Publishing files stored in the file system through external list).

Up to know I found no easy way to look inside the real content of such a web part. You can import the solution into Visual Studio 2010 (see example here), but I feel it a bit uncomfortable. If you can attach to the server using SharePoint Designer 2010, you can check the content there, but sometimes you might be offline, for example, the WSP was created in a restricted network and was sent via e-mail.

The following snippet illustrates a BinarySerializedWebPart:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <Module Name="FileSystemForms" Url="Lists/Files" RootWebOnly="FALSE" SetupPath="pages">
  4.     <File Url="DispForm.aspx" Type="Ghostable" Path="form.aspx">
  5.       <BinarySerializedWebPart>
  6.         <GUIDMap>
  7.           <GUID Id="33ff2881_489d_4ce2_ac94_e81d64689d2a" ListUrl="Lists/Files" />
  8.         </GUIDMap>
  9.         <WebPart ID="{035cec7d-5f69-4dbf-a551-0b8203467c41}" WebPartIdProperty="" List="{$ListId:Lists/Files;}" Type="4"
  10.           Flags="0" DisplayName="" Version="4" Url="Lists/Files/DispForm.aspx" WebPartOrder="1" WebPartZoneID="Main"
  11.           IsIncluded="True" FrameState="0" WPTypeId="{feaafd58-2dc9-e199-be37-d6cdd7f84690}"
  12.           SolutionId="{00000000-0000-0000-0000-000000000000}" Assembly="" Class="" Src=""
  13.           AllUsers="B6Dt/kMAAAABAAAAAAAAAAIAAAAvX2xheW91dHMvaW1hZ2VzL2l0ZWJsLnBuZwAvRjFTaXRlL0xpc3RzL0ZpbGVzAP8BFCsAJQICAgMCAwEEAAICAhICFAEBAAIEBQtDb250cm9sTW9kZQspiAFNaWNyb3NvZnQuU2hhcmVQb2ludC5XZWJDb250cm9scy5TUENvbnRyb2xNb2RlLCBNaWNyb3NvZnQuU2hhcmVQb2ludCwgVmVyc2lvbj0xNC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj03MWU5YmNlMTExZTk0MjljAQUIRm9ybVR5cGUCBAEAAAIWAoYBCyo0U3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5XZWJQYXJ0cy5XZWJQYXJ0RXhwb3J0TW9kZQICggEFGi9fbGF5b3V0cy9pbWFnZXMvaXRlYmwucG5nAn0FEy9GMVNpdGUvTGlzdHMvRmlsZXMFCFBhZ2VUeXBlCyl3TWljcm9zb2Z0LlNoYXJlUG9pbnQuUEFHRVRZUEUsIE1pY3Jvc29mdC5TaGFyZVBvaW50LCBWZXJzaW9uPTE0LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTcxZTliY2UxMTFlOTQyOWMEBQdMaXN0VXJsZQUGTGlzdElkKClYU3lzdGVtLkd1aWQsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Y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yaWFibGUiIERlZmF1bHRWYWx1ZT0iQ3VycmVudERhdGUiLz4NCiAgICAgICAgPFBhcmFtZXRlckJpbmRpbmcgTmFtZT0iTGlzdEl0ZW1JZCIgTG9jYXRpb249IlF1ZXJ5U3RyaW5nKElEKSIgRGVmYXVsdFZhbHVlPSIwIi8+DQogICAgICAgIA==" />
  14.       </BinarySerializedWebPart>
  15.       </File>
  16.     </Module>
  17. </Elements>

You can see at the first sight that the most critical part of the XML is the AllUsers attribute of the WebPart node.

Other BinarySerializedWebPart nodes may contain View or PerUser attributes with similar format, and another attribute, WPTypeId that is a GUID.

In this post I try to help you to understand the decoding (or we can call it deserialization but it is not really deserialization in its traditional meaning) process SharePoint applies when reading web parts from content database and on the other side the encoding (or serialization) of the web part when creating the WSP package on site template export.

If you don’t like to track call chains you might want to skip the following section although I think it may help to understand the whole story and gives you a picture about what happens under the cover of the export process and some other areas related to the solution of the problem, like how web parts are reconstructed from content database by a web part manager. In this case you can check my next post that contains the actual code.

First, the public static ExportWeb method of SPSolutionExporter class (Microsoft.SharePoint namespace in Microsoft.SharePoint assembly) is called. The sole task of this method is to instantiate a SPSolutionExporter object to call its private ExportWebAsSolution method. This method calls forward to another private method GenerateSolutionFiles and this later one calls the private ExportWebWebPart method calls the proc_EnumerateWebPartsForWeb stored procedure and reads the web parts from the database including the properties we need.

If you are a regular visitor of the SharePoint content database you may be familiar with the AllWebParts table and its fields. The four fields we focus on are neighbors in this table:  tp_View, tp_WebPartTypeId, tp_AllUsersProperties, tp_PerUserProperties. These fields are bound to the above-mentioned attributes.

The ExportWebWebPart method aggregates the web parts in the WebWebParts property (that is of type SortedList<string, List<WebWebPart>>) of the SPSolutionExporter class.

The information about the web part is stored in the members of the private WebWebPart class, the relevant ones are:

  1. public byte[] AllUsersProperties;
  2. public byte[] PerUserProperties;
  3. public byte[] View;
  4. public Guid WebpartTypeId;

Later the GenerateSolutionFiles method calls ExportModules and ExportLists methods, then both of these methods call the WriteModuleInnerElementsOfElements method. This method calls the WriteModuleElementIntoFeatureManifest method: once for the customized and once for the uncustomized entries. In this method the EmitAllUsersWebpart method is called, that calls forward into the WriteBlobWebpart method.

In WriteBlobWebpart method you can see that most of the attributes are written as simple strings, however byte arrays are written as Base64 encoded values. It means that the attributes with more complex values contain the same binary context as their corresponding database fields in the AllWebParts table.

OK, we already know that the values of these properties must be Base64 decoded to get a byte array. That is not a surprise for most of us, it could have been said at first sight having a bit of experience in Base64. But the question still remains: how could we reconstruct a web part using these binary values?

To answer the question, let’s see what happens internally in the SPWebPartManager (Microsoft.SharePoint.WebPartPages namespace, Microsoft.SharePoint assembly) when it reads the web parts from the content database.

For the sake of simplicity I list only the methods involved in the process as their follow each other, and limit myself only to the methods that lead us to the “decoding algorithm”:

OnInit -> OnPageInitComplete -> LoadWebParts -> CreateWebPartsFromRowSetData

The CreateWebPartsFromRowSetData method calls the internal static  CreateBinaryDeserializer method of the internal  BinaryWebPartDeserializer class (Microsoft.SharePoint.WebPartPages namespace in Microsoft.SharePoint assembly) that returns an instance of BinaryWebPartDeserializer or its derived internal class SPUserCodeWebPartDeserializer. Then its internal Deserialize method is called that finally returns a WebPart instance.

When you follow the call chain in Reflector, at this point you might be a bit misled, as when you click on the Deserialize method in the CreateWebPartsFromRowSetData method, the virtual Deserialize method of the BinaryWebPartDeserializer class is displayed, however, since the SPUserCodeWebPartDeserializer class might be instantiated within the CreateBinaryDeserializer method, you may have to follow the override Deserialize method of the SPUserCodeWebPartDeserializer class. The first step in this method is to call the Deserialize method of the base class.

The Deserialize method of the BinaryWebPartDeserializer class calls the LoadInitialWebPart method that decodes the byte array properties by calling the ByteArrayToObjectArray method of the BinaryWebPartSerialization class (Microsoft.SharePoint.WebPartPages namespace, Microsoft.SharePoint assembly). This method then calls the static DeserializeByteArrayToObject method (same namespace and assembly as former one). Finally the Deserialize and DeserializeValue methods of the ObjectStateFormatter class (System.Web.UI namespace, System.Web assembly) do the actual work of reading object from stream.

Important thing to note. The Deserialize method contains a check that the first byte of the stream must be 255, and the second one must be 1, otherwise an exception of “The serialized data is invalid” will be thrown. The binary data stored in the fields of AllWebParts table do not always start with these bytes, similar to the Base64 decoded XML attributes of the BinarySerializedWebPart node.

In my tests I simply ignored the bytes in the byte array before this specific byte pattern, and using the code showed that splitting the leading bytes produces a useable result. How the actual code does look like will be shown in the next part of the post.

Blog at WordPress.com.