0

Is it possible to check two SharePoint lists are identical in terms of list columns(Column names and types) programmatically?

1

Yes that is possible. E.g. I believe the PNP framework uses this to check what it needs to deploy.

This sample code is from: https://www.sharepointsky.com/sharepoint-2016-csom-check-if-column-exists-or-not-in-sharepoint-list/

private void btnOnPremise_Click(object sender, EventArgs e)
{
    using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
    {
        var list = context.Web.Lists.GetByTitle("MyDemoList");
        context.Load(list);
        context.Load(list.Fields);
        context.ExecuteQuery();
        for (int i = 0; i < list.Fields.Count; i++)
        {
            if (list.Fields[i].Title == "MyTestColumn")
            {
                label1.Text = "Column Exists";
                return;
            }
        }
    }
}
  • most of the things are fine. But how this code compares field type? – Sivakumar Piratheeban Oct 8 '18 at 17:08
0

I think easiest option would be to save both lists as templates, that will give you 2 stp files. Now you can rename the STP file and to .CAB and open it. There you will have manifest file you can now compare them to see the difference.

0

I don't think there is a perfect list matching function but a good option could be to compare the SchemaXML of the two lists. In this example you can compare two lists with powershell. The point is to remove GUID during compare. Maybe this compare isn't perfect when comparing lookup fields.

function resumeList($list) {

    $totschema = "";
    $list.fields | sort InternalName | % {
        $f = $_; 
        $schema = $f.schemaxml -replace '="\{[a-z0-9\-]*\}"'; 
        $totschema += $schema; 
    }
    return $totschema

}

function compareLists($weburl1, $listtitle1, $weburl2, $listtitle2) {

    $web1 = get-spweb($weburl1);
    $list1 = $web1.lists.item($listtitle1);
    $resumeList1 = resumeList $list1
    $web2 = get-spweb($weburl2);
    $list2 = $web2.lists.item($listtitle2);
    $resumeList2 = resumeList $list2
    return ($resumeList1 -eq $resumeList2)

}


$weburl1 = "http://test/sites/ticket/web1"
$listtitle1 = "Ticket"
$weburl2 = "http://test/sites/ticket/web2"
$listtitle2 = "TicketX"
$result = compareLists $weburl1 $listtitle1 $weburl2 $listtitle2
if ($result) {
    "Lists are equals"
} else {
    "Lists are different"
}

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.