Saturday, June 2, 2012

Avoid duplicate files into Document library in sharepoint 2010

Here i will show how to prevent the same file to be uploaded multiple times into document library in share point 2010.This process will apply when the Itemadding event occurred.To get the all the details about list items events in share point website we have to use excute a query to get listitemcollection.The file name is getting from url then it will pass to spquery and get the count of files with this file name,then check the file is already exist or not .
//Before url is get the url of item before event
string fRawUrl = p.BeforeUrl;
string fName = fRawUrl.Substring(fRawUrl.LastIndexOf('/') + 1);
slist=p.List;
SPQuery squery = new SPQuery();
squery.Query = string.Format("<where><eq><fieldref name="FileLeafRef"></fieldref><value type="Text">Docname</value></eq></where>",fName);
SPListItemCollection sitem = slist.GetItems(query);
//check wether the file is already existing or not 
if (sitem.Count > 0)
{
p.Cancel = true;
p.ErrorMessage="Item already existed"
}
Post a Comment

Bel