Compare InfoPath Versions in a SharePoint 2010 Library with Major Versioning Enabled - sharepoint-2010

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;
}

Related

Google diff-match-patch : How to unpatch to get Original String?

I am using Google diff-match-patch JAVA plugin to create patch between two JSON strings and storing the patch to database.
diff_match_patch dmp = new diff_match_patch();
LinkedList<Patch> diffs = dmp.patch_make(latestString, originalString);
String patch = dmp.patch_toText(diffs); // Store patch to DB
Now is there any way to use this patch to re-create the originalString by passing the latestString?
I google about this and found this very old comment # Google diff-match-patch Wiki saying,
Unpatching can be done by just looping through the diff, swapping
DIFF_INSERT with DIFF_DELETE, then applying the patch.
But i did not find any useful code that demonstrates this. How could i achieve this with my existing code ? Any pointers or code reference would be appreciated.
Edit:
The problem i am facing is, in the front-end i am showing a revisions module that shows all the transactions of a particular fragment (take for example an employee details), like which user has updated what details etc. Now i am recreating the fragment JSON by reverse applying each patch to get the current transaction data and show it as a table (using http://marianoguerra.github.io/json.human.js/). But some JSON data are not valid JSON and I am getting JSON.parse error.
I was looking to do something similar (in C#) and what is working for me with a relatively simple object is the patch_apply method. This use case seems somewhat missing from the documentation, so I'm answering here. Code is C# but the API is cross language:
static void Main(string[] args)
{
var dmp = new diff_match_patch();
string v1 = "My Json Object;
string v2 = "My Mutated Json Object"
var v2ToV1Patch = dmp.patch_make(v2, v1);
var v2ToV1PatchText = dmp.patch_toText(v2ToV1Patch); // Persist text to db
string v3 = "Latest version of JSON object;
var v3ToV2Patch = dmp.patch_make(v3, v2);
var v3ToV2PatchTxt = dmp.patch_toText(v3ToV2Patch); // Persist text to db
// Time to re-hydrate the objects
var altV3ToV2Patch = dmp.patch_fromText(v3ToV2PatchTxt);
var altV2 = dmp.patch_apply(altV3ToV2Patch, v3)[0].ToString(); // .get(0) in Java I think
var altV2ToV1Patch = dmp.patch_fromText(v2ToV1PatchText);
var altV1 = dmp.patch_apply(altV2ToV1Patch, altV2)[0].ToString();
}
I am attempting to retrofit this as an audit log, where previously the entire JSON object was saved. As the audited objects have become more complex the storage requirements have increased dramatically. I haven't yet applied this to the complex large objects, but it is possible to check if the patch was successful by checking the second object in the array returned by the patch_apply method. This is an array of boolean values, all of which should be true if the patch worked correctly. You could write some code to check this, which would help check if the object can be successfully re-hydrated from the JSON rather than just getting a parsing error. My prototype C# method looks like this:
private static bool ValidatePatch(object[] patchResult, out string patchedString)
{
patchedString = patchResult[0] as string;
var successArray = patchResult[1] as bool[];
foreach (var b in successArray)
{
if (!b)
return false;
}
return true;
}

Deploying SSRS RDL files from VB.Net - Issue with shared datasources

I am currently developing a utility to help automate our report deployment process. Multiple files, in multiple folders, to multiple servers.
I am using the reportservice2010.asmx web service, and I am deploying my files to the server - so most of the way there.
My issue is that I have shared data sets and shared data sources, which are deployed to individual folders, separate to the report folders. When the deployment occurs the web service looks locally for the data source rather than in the data source folder, giving an error like:
The dataset ‘CostReduction’ refers to the shared data source ‘CostReduction’, which is not
published on the report server. The shared data source ‘CostReduction’ must be published
before this report can run.
The data source/set has been deployed and the report functions correctly but I need to suppress these error messages as they may be hiding other actual errors.
I can hard code a lookup that checks if the data source/set exists and manually filter them via that, but it seems very in-efficient. Is there any way I can tell the web service where to look for these files or another approach that other people have used?
I'm not looking at changing the reports so the data source is read from
/DataSources/DataSourceName
as there are lots of reports and that's not how our existing projects are configured.
Many thanks in advance.
I realize you are using VB, but perhaps this will give you a clue if you convert it from C# to VB, using one of the translators on the web.
Hopefully this will give you a lead in the right direction.
When All the reports in a particular folder, referred to here as the 'parent folder', all use the same Shared Data source, I use this to set all the reports to the same shared Data Source (in this case "/DataSources/Shared_New")
using GetPropertiesSample.ReportService2010;
using System.Diagnostics;
using System.Collections.Generic; //<== required for LISTS
using System.Reflection;
namespace GetPropertiesSample
{
class Program
{
static void Main(string[] args)
{
GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource("0_Contacts"); //<=== This is the parent folder
}
private static void GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource(string sParentFolder)
{
// Create a Web service proxy object and set credentials
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
CatalogItem[] reportList = rs.ListChildren(#"/" + sParentFolder, true);
int iCounter = 0;
foreach (CatalogItem item in reportList)
{
iCounter += 1;
Debug.Print(iCounter.ToString() + "]#########################################");
if (item.TypeName == "Report")
{
Debug.Print("Report: " + item.Name);
ResetTheDataSource_for_a_Report(item.Path, "/DataSources/Shared_New"); //<=== This is the DataSource that I want them to use
}
}
}
private static void ResetTheDataSource_for_a_Report(string sPathAndFileNameOfTheReport, string sPathAndFileNameForDataSource)
{
//from: http://stackoverflow.com/questions/13144604/ssrs-reportingservice2010-change-embedded-datasource-to-shared-datasource
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string reportPathAndName = sPathAndFileNameOfTheReport;
//example of sPathAndFileNameOfTheReport "/0_Contacts/207_Practices_County_CareManager_Role_ContactInfo";
List<ReportService2010.ItemReference> itemRefs = new List<ReportService2010.ItemReference>();
ReportService2010.DataSource[] itemDataSources = rs.GetItemDataSources(reportPathAndName);
foreach (ReportService2010.DataSource itemDataSource in itemDataSources)
{
ReportService2010.ItemReference itemRef = new ReportService2010.ItemReference();
itemRef.Name = itemDataSource.Name;
//example of DataSource i.e. 'itemRef.Reference': "/DataSources/SharedDataSource_DB2_CRM";
itemRef.Reference = sPathAndFileNameForDataSource;
itemRefs.Add(itemRef);
}
rs.SetItemReferences(reportPathAndName, itemRefs.ToArray());
}
}
To Call it I use this in the 'Main' Method:
GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource("0_Contacts");
In this case "0_Contacts" is the parent folder, itself located in the root directory, that contains all the reports for which I want to reset their DataSources to the new Shared DataSource. Then that Method calls the other method "ResetTheDataSource_for_a_Report" which actually sets the DataSource for the report.

Sitecore Glass mapper GetItem<TypeName>(guid) always return null

I saw a related question:
Sitecore Glass Mapper always null
But unfortunately it does not give a solution for my case.
Here goes a code snippet:
var db = Factory.GetDatabase("master");
var context = new SitecoreContext();
// the ID of Needed item
var g = new Guid("{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76}");
// just to test if it's possible to fetch item using db.GetItem
var i = db.GetItem(new ID(g), Language.Current, Sitecore.Data.Version.Latest);
// Grab item
var t = context.GetItem<Article>(g);
In the code above:
i is not null
t is null
Article is the simple class like:
[SitecoreType(TemplateId = "{4C4EC1DA-EB77-4001-A7F9-E4C2F61A9BE9}")]
public class Article
{
[SitecoreField(FieldName = "Title")]
public string Title { get; set; }
}
There are only one language installed in Sitecore - en, it has been specified in the web.config in the items as well.
Also I have added GlassMapperSc.Start(); to Application_Start in the Global.asax.cs and added my assembly to the list of included assemblies via var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" }); and I succeeded to find my class in the SitecoreContext mappings.
It does not looks like a language issue, as stated in the link provided in the very beginning. And I'm struggling with it already for a pretty long time, but no luck...
Thank You!
I just noticed that you are using master db for the Sitecore DB and SitecoreContext for Glass.
The SitecoreContext class will use the database that is defined by the Sitecore.Context.Database property at runtime. This probably means that it is using the web database.
Can you check that you have published the item to the web database or instead using:
var context = new SitecoreService("master");

Problem saving a new item to custom SharePoint 2007 list using Silverlight 4 UI control

I am hoping someone might be able to help me. I'm working on a Contact Manager built using a custom SharePoint 2007 list with a Silverlight 4 UI embedded in a content editor web part.
I am currently able to retrieve the data from the list and display it in a datagrid on the UI and everything works well.
Now I am trying to add the the ability to add new items to the list using the following code but the items do not save.
I've remotely debugged the following code using the Debug -> Attach to Process option and everything seems to execute successful without any errors but it does not save the item to SharePoint.
In order to simplify and get a working insert function I changed all the SharePoint fieds to single line text with the exception of the notes (multiline) and none of the fileds are required.
The sharepoint site does require Windows authentication but it seems to be working correctly as I am able to display it as well as add new items manually using the standard SharePoint forms.
Lastly, I have added the xml for the Batch element at the bottom which I copied as output while debuging.
Please let me know if there is any additional information I might be missing.
Thanks in advance for any assistance you might be willing to provide.
Charles
public string sharepoint_soap_namespace = "http://schemas.microsoft.com/sharepoint/soap/";
public string sharepoint_rowset_namespace = "#RowsetSchema";
public string service_lists_url = "http://myDomain/_vti_bin/lists.asmx";
public string listName = "MyContacts";
public void TestCreateContact()
{
Uri serviceUri = new Uri(service_lists_url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = 2147483647; // This has to be the same as in the ServiceReferences.ClientConfig file.
EndpointAddress endpoint = new EndpointAddress(serviceUri);
ListsSoapClient testCreateClient = new ListsSoapClient(binding, endpoint);
XElement batch = new XElement("batch",
new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "New"),
CreateFieldElement("ows_ID", "New"),
CreateFieldElement("ows_Title", "John"),
CreateFieldElement("ows_SupportFor","USA"),
CreateFieldElement("ows_LastName","Doe")
));
testCreateClient.UpdateListItemsCompleted +=
new EventHandler<UpdateListItemsCompletedEventArgs>(createSoapClient_UpdateListItemsCompletedEventArgs);
testCreateClient.UpdateListItemsAsync(listName, batch);
testCreateClient.CloseAsync();
}
private XElement CreateFieldElement(string fieldName, string fieldValue)
{
XElement element = new XElement("Field",
new XAttribute("Name", fieldName),
fieldValue);
return element;
}
Just a quick update to let everyone know I was able to answer my own question.
It seems that in the batch XElement I was using the wrong field names.
CreateFieldElement("ows_SupportFor","USA"),
I was using "ows_SupportFor" instead of "SupportFor" without the "ows_" prefix.
Cheers,
Charles

ASP.NET Site Maps

Does anyone have experience creating SQL-based ASP.NET site-map providers?
I have the default XML file web.sitemap working properly with my Menu and SiteMapPath controls, but I'll need a way for the users of my site to create and modify pages dynamically.
I need to tie page viewing permissions into the standard ASP.NET membership system as well.
The Jeff Prosise version from MSDN magazine works pretty well, but it has a few flaws:
AddNode freaks out with links to external sites on your menu (www.google.com, etc.)
Here's my fix in BuildSiteMap():
SiteMapNode node = GetSiteMapNodeFromReader(reader);
string url = node.Url;
if (url.Contains(":"))
{
string garbage = Guid.NewGuid().ToString(); // SiteMapNode needs unique URLs
node.Url = "~/dummy_" + garbage + ".aspx";
AddNode(node, _root);
node.Url = url;
}
else
{
AddNode(node, _root);
}
SQLDependency caching is cool, but if you don't want to make a trip to the DB everytime your menu loads (to check to see if the dependency has changed) and your menus don't change very often, then why not use HttpRuntime.Cache instead?
public override SiteMapNode RootNode
{
get
{
SiteMapNode temp = (SiteMapNode)HttpRuntime.Cache["SomeKeyName"];
if (temp == null)
{
temp = BuildSiteMap();
HttpRuntime.Cache.Insert("SomeKeyName", temp, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
}
return temp;
}
}