Skip to main content

Document Check in for Add Actions

To automatically check-in SharePoint Documents after they have been created you will need to use Project Automation Item Events with the SharePoint API to check-in the document.

To get started add the following code to the Project Automation class to get a reference to the SharePoint Lists Web Service.

private Simego.DataSync.ListWS.Lists _listService;

private Simego.DataSync.ListWS.Lists ListService
{
get
{
if(_listService == null)
{
_listService = DataSourceB.GetSharePointListService();
}
return _listService;
}
}

Then you need to add code to the AfterAddItem method to check in the document after it is uploaded to SharePoint.

This code creates the URL from the SharePoint connection configuration and uses the FullFileName column from the source schema, this maybe different depending on the connector you are using.

info

Checking in a Document will update the Modified column to time of the check in so this will always be different to your source value.

public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
var filename = (string)item.SourceRow.Get("FullFileName").BeforeColumnValue;
string fileCheckin = Utility.CombineWebPath(DataSourceB.SharePointUrl, Utility.CombineWebPath(DataSourceB.ListName, filename));
Trace.WriteLine(fileCheckin);
// fileUrl - A string that contains the full path to the document to check in.
// comment - A string containing optional check-in comments.
// CheckinType - A string representation of the values 0, 1 or 2, where 0 = MinorCheckIn, 1 = MajorCheckIn, and 2 = OverwriteCheckIn.

ListService.CheckInFile(fileCheckin, "Uploaded by DataSync", "0");
}

Check Out and Check In for Update Actions

If you also need to support checkout before an Update and then check in after the upload you can implement something similar with the Before and After Item Events.

        public override void BeforeUpdateItem(object sender, DataCompareItemInvariant item, object identity)
{
var filename = (string)item.SourceRow.Get("FullFileName").BeforeColumnValue;
string fileCheckout = Utility.CombineWebPath(DataSourceB.SharePointUrl, Utility.CombineWebPath(DataSourceB.ListName, filename));

var lastModified = (DateTime)item.SourceRow.Get("DateModified").AfterColumnValue;
var lastModifiedRFC1123 = lastModified.ToUniversalTime().ToString("r");

Trace.WriteLine(string.Concat(fileCheckout, "->", lastModifiedRFC1123));

var result = ListService.CheckOutFile(fileCheckout, "true", lastModifiedRFC1123);

if(result == false)
{
throw new ApplicationException("Unable to Checkout File " + fileCheckout);
}
}

public override void AfterUpdateItem(object sender, DataCompareItemInvariant item, object identity)
{
var filename = (string)item.SourceRow.Get("FullFileName").BeforeColumnValue;
string fileCheckin = Utility.CombineWebPath(DataSourceB.SharePointUrl, Utility.CombineWebPath(DataSourceB.ListName, filename));

Trace.WriteLine(fileCheckin);

ListService.CheckInFile(fileCheckin, "Uploaded by DataSync", "0");
}

This uses the SharePoint API SharePoint API to call the CheckOutFile Web API on the SOAP List Service.