jeudi 8 mai 2014

Comparer les Versions d'InfoPath dans une bibliothèque SharePoint de 2010 avec gestion des versions principales activée - débordement de pile


I have an InfoPath 2010 form that is published as a content type. I have a SharePoint library that has this content type enabled. The SharePoint library also have major versioning enabled.


Let's say that I have saved an instance of the form in the library and edited it multiple times so that multiple versions are created.


I need to compare 2 versions against each other to see the exact changes. Is this supported by SharePoint or should I used code for that (I am examining SPDiffUtility now)?




No, SharePoint does not let you compare versions and their differences OOTB.


As far as I can tell SPDiffUtility simply tells you the difference between two strings, but does not support versions just like that. Comparing version is still very easy:


using (SPWeb web = new SPSite("http://sharepoint").OpenWeb())
{
SPList list= web.Lists["Shared Documents"];

SPFile file = list.Files["mydoc.doc"];

//Get all the versions
SPFileVersionCollection fileVersionCollection = file.Versions;

//Get the first version
SPFileVersion fileVersion= fileVersionCollection[3];

//Get the data
byte [] fileBytes = version.OpenBinary();
}

Basically you have to look into the SPFile.Versions collection and compare the versions you have.


The problem is that InfoPath stores its document as XML, so you will have to parse the XML you receive to extract all fields and see their differences - a good start for parsing the XML is to create a class file for easier access in code via xsd.exe like for example explained here.




For reference, here is the complete code I used to compare 2 versions of an InfoPath form


private static void CompareVersions()
{
using (SPWeb web = new SPSite("http://<website_name>").OpenWeb())
{
SPList lib = web.Lists["<library_name>"];

// Assuming that the file has at least 2 versions
var v1 = lib.RootFolder.Files[0].Versions[0];
myFields i1 = GetInstanceFromVersion(v1);
var v2 = lib.RootFolder.Files[0].Versions[1];
myFields i2 = GetInstanceFromVersion(v2);

Console.WriteLine(string.Format("{0,-20} | {1,-20} | {2,-20}", "Version", v1.VersionLabel, v2.VersionLabel));
// List the properties of both versions
Console.WriteLine(string.Format("{0,-20} | {1,-20} | {2,-20}", "Name", i1.Name, i2.Name));
}
}

private static myFields GetInstanceFromVersion(SPFileVersion version)
{
XmlTextReader reader = new XmlTextReader(version.OpenBinaryStream());
myFields fields = (myFields)new XmlSerializer(typeof(myFields)).Deserialize(reader);
return fields;
}


I have an InfoPath 2010 form that is published as a content type. I have a SharePoint library that has this content type enabled. The SharePoint library also have major versioning enabled.


Let's say that I have saved an instance of the form in the library and edited it multiple times so that multiple versions are created.


I need to compare 2 versions against each other to see the exact changes. Is this supported by SharePoint or should I used code for that (I am examining SPDiffUtility now)?



No, SharePoint does not let you compare versions and their differences OOTB.


As far as I can tell SPDiffUtility simply tells you the difference between two strings, but does not support versions just like that. Comparing version is still very easy:


using (SPWeb web = new SPSite("http://sharepoint").OpenWeb())
{
SPList list= web.Lists["Shared Documents"];

SPFile file = list.Files["mydoc.doc"];

//Get all the versions
SPFileVersionCollection fileVersionCollection = file.Versions;

//Get the first version
SPFileVersion fileVersion= fileVersionCollection[3];

//Get the data
byte [] fileBytes = version.OpenBinary();
}

Basically you have to look into the SPFile.Versions collection and compare the versions you have.


The problem is that InfoPath stores its document as XML, so you will have to parse the XML you receive to extract all fields and see their differences - a good start for parsing the XML is to create a class file for easier access in code via xsd.exe like for example explained here.



For reference, here is the complete code I used to compare 2 versions of an InfoPath form


private static void CompareVersions()
{
using (SPWeb web = new SPSite("http://<website_name>").OpenWeb())
{
SPList lib = web.Lists["<library_name>"];

// Assuming that the file has at least 2 versions
var v1 = lib.RootFolder.Files[0].Versions[0];
myFields i1 = GetInstanceFromVersion(v1);
var v2 = lib.RootFolder.Files[0].Versions[1];
myFields i2 = GetInstanceFromVersion(v2);

Console.WriteLine(string.Format("{0,-20} | {1,-20} | {2,-20}", "Version", v1.VersionLabel, v2.VersionLabel));
// List the properties of both versions
Console.WriteLine(string.Format("{0,-20} | {1,-20} | {2,-20}", "Name", i1.Name, i2.Name));
}
}

private static myFields GetInstanceFromVersion(SPFileVersion version)
{
XmlTextReader reader = new XmlTextReader(version.OpenBinaryStream());
myFields fields = (myFields)new XmlSerializer(typeof(myFields)).Deserialize(reader);
return fields;
}

0 commentaires:

Enregistrer un commentaire