In two of my previous posts I discussed how
to get, delete and resore file versions programmatically.
I suggested way of getting versions using web
service. Now let’s do the same thing with Silverlight client object model. Same
thing can be done with JavaScript object model with some syntax changes.
Here I am considering same scenarios which we
considered when we used web service. To put this in other way I have written
sample methods for the operations which are possible by client object model.
These are some sample methods:
I
have placed clientcontext object to App level. So I am using App.clientContext
in my code.
Remember
to load clientcontext before using these methods.
Get
all versions:
private void
GetAllFileVersions(string fileUrl)
{
FileVersionCollection
fileVersionCollection;
file = App.clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
fileVersionCollection = file.Versions;
App.clientContext.Load(file);
App.clientContext.Load(fileVersionCollection);
App.clientContext.ExecuteQueryAsync((s,
args) =>
{
int
numberOfVersions = fileVersionCollection.Count;
},
(s, args) =>
{
//"Error loading versions.
});
}
Usage:
GetAllFileVersions("/sites/prasad/teamsite/MyLibrary/testexcel.xlsx");
Description:
This method will load file version
collection. You can iterate through each file version and get values for
various properties which will give you date of creation, user object of user
who created version, url of file for that version and Boolean value that tells if
it is a current version of file.
Delete Specific Version
Using Version ID:
private void
DeleteSpecificFileVersion(string fileUrl, int versionId)
{
file = App.clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
App.clientContext.Load(file);
file.Versions.DeleteByID(versionId);
App.clientContext.ExecuteQueryAsync((s, args)
=>
{
//"Deleted file version successfuly.
},
(s,
args) =>
{
//"Error deleting file version.
});
}
Usage:
DeleteSpecificFileVersion("/sites/prasad/teamsite/MyLibrary/testexcel.xlsx",512);
Description:
This method deletes specific version
of file when version Id is provided. Interesting fact here is what is version
id?
While iterating through versions of
file you can get version id from “ID” property of “FileVersion” class.
Version ids are in multiple of
512.i.e. for first version it is 512, for second version it is 1024, for third
version it is 1536 and so on.
Delete Specific
Version Using Version Label:
private void
DeleteSpecificFileVersion(string fileUrl, string versionLabel)
{
file = App.clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
App.clientContext.Load(file);
file.Versions.DeleteByLabel(versionLabel);
App.clientContext.ExecuteQueryAsync((s, args)
=>
{
//"Deleted file version successfuly.
},
(s, args) =>
{
//"Error deleting file version.
});
}
Usage:
DeleteSpecificFileVersion("/sites/prasad/teamsite/MyLibrary/testexcel.xlsx",
"2.0");
Description:
This method deletes specific version
of file when version label is provided. Version label means version number
which you see on UI. i.e. “1.0”, “2.0”,”3.0” and so on.
While iterating through versions of
file you can get version label from “VersionLabel” property of “FileVersion”
class.
Delete All File
Versions:
private void
DeleteAllFileVersions(string fileUrl)
{
file = App.clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
App.clientContext.Load(file);
file.Versions.DeleteAll();
App.clientContext.ExecuteQueryAsync((s, args)
=>
{
//"Deleted all file versions successfuly.
},
(s, args) =>
{
//"Error deleting old file versions.
});
}
Usage:
DeleteAllFileVersions("/sites/prasad/teamsite/MyLibrary/testexcel.xlsx");
Description:
This simply deletes all the versions
of file but current version.
Restore Specific
Version:
private void
RestoreSpecificFileVersion(string fileUrl, string versionLabel)
{
file = App.clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
App.clientContext.Load(file);
file.Versions.RestoreByLabel(versionLabel);
App.clientContext.ExecuteQueryAsync((s, args)
=>
{
//"Restored file version successfuly.
},
(s, args) =>
{
//"Error restoring old file versions.
});
}
Usage:
RestoreSpecificFileVersion("/sites/prasad/teamsite/MyLibrary/testexcel.xlsx",
"3.0");
Description:
This method restores specific version
of file when version label is provided.
To get current version of file
use “UIVersion” or “UIVersionLabel” properties of “File”
class. These properties will return you version id and version label
respectively.
Interesting fact is version ids which
SharePoint maintains. As I mentioned earlier, these are in multiples of “512”.
URL of file version contains this version id in it. E.g.–
URL
of first Version: http://[My
Site URL]/_vti_history/512/MyLibrary/testexcel.xlsx
URL
of second Version: http://[My Site
URL]/_vti_history/1024/MyLibrary/testexcel.xlsx
URL
of third Version: http://[My
Site URL]/_vti_history/1536/MyLibrary/testexcel.xlsx
Getting this path is nothing fancy. If you
look at the version history of any file you can get this.
Just mouse over the link for each version and
observe URL, you will get URL in above mention format.
Most important thing, current version of file
will not include version id in its URL.
I learned many things from above exercise. Did
you?