Monday, March 29, 2010

UIVersionLabel property of file object, yes this is the property which will give you the latest version of a file stored in a version enabled document liabrary. so steps are ;

1- Create SPSite object
2- Get SWeb object.
3- Get the library
4- Get the file and then use UIVersionlable property to get the latest version.



public void getLastVersion()
{
string doclibname = ConfigurationManager.AppSettings["doclibname"].ToString();
string fname = "filename.doc";


bool blnFound = false;
//get the latest version
using (SPSite objSite = SPContext.Current.Site)
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPFolder mylibrary = objWeb.Folders[doclibname];
foreach (SPFile file in mylibrary.Files)
{
if (file.Name.ToString() == fname + ".doc")
{
blnFound = true;
lblVersion.Text = file.UIVersionLabel;
lblDate.Text = file.TimeLastModified.ToString("MM/dd/yyyy");
}
}
if (blnFound == false)
{
lblVersion.Text = "Not Available";
lblDate.Text = "Not Available";
}

}
}
}


Thanks
Sanjay Tiwari

Thursday, March 25, 2010

As usual, you need to reference liabraries System.Configuration to access AppSettings and Microsoft.Sharepoint for sharepoint object model

public void DeleteFile(string FinalFileName)
{
string s_FileName = "";
string doclibname = ConfigurationManager.AppSettings["doclibname"].ToString();
SPSite objSite = SPContext.Current.Site;
SPWeb objWeb = objSite.OpenWeb();
SPFolder mylibrary = objWeb.Folders[doclibname];
foreach (SPFile file in mylibrary.Files)
{
if (file.Name.ToString().ToUpper() == FinalFileName.ToUpper())
{
objWeb.AllowUnsafeUpdates = true;
file.Delete();
mylibrary.Update();
break;

}
}

}