none
How to prevent duplicate files to be uploaded in to Document libray in sharepoint2010

    Question

  • Hi all

    I want to  prevent to add duplicate files to document library in sharepoint 2010. when we are uploading document it will verify the all Doclib Files then if file name is exists it will show alertbox.

      I know this can be solve with event receivers but i new to Sharepoint i need some code

    how to do that ?

    pls help me and I need some code

     

    • Moved by Mike Walsh FIN Tuesday, July 20, 2010 2:46 AM (2) 2010 questions all go to a suitable 2010 forum. A suitable 2010 forum is not one with "Pre-SharePoint 2010" in the Title (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    Monday, July 19, 2010 4:29 PM

Answers

  • Hi Raju T,

     

    Thanks for your post.

     

    There are some different from developing Event handler in sharepoint 2007, so I suggest you to read the reference at first:

    http://philwicklund.com/archive/2009/10/21/building-an-event-handler-and-custom-error-page-in-sharepoint-2010.aspx

     

    Then I have written some sample code for this issue:

    /// <summary>

           /// An item is being added.

           /// </summary>

           public override void ItemAdding(SPItemEventProperties properties)

           {

               string fileUrl = properties.BeforeUrl;

               string fileName = fileUrl.Substring(fileUrl.LastIndexOf('/') + 1);

               if (IsExisted(properties.List, fileName))

               {

                     properties.Cancel = true;

                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

                   properties.RedirectUrl = "/_Layouts/MyEventError.aspx";

     

     

               }

           }

     

           /// <summary>

           /// An item is being updated.

           /// </summary>

           public override void ItemUpdating(SPItemEventProperties properties)

           {

               string fileUrl = properties.BeforeUrl;

               string fileName = fileUrl.Substring(fileUrl.LastIndexOf('/') + 1);

               if (IsExisted(properties.List, fileName))

               {

                   properties.Cancel = true;

                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

                   properties.RedirectUrl = "/_Layouts/MyEventError.aspx";

               }

           }

     

           private Boolean IsExisted(SPList list, string fileName)

           {

               bool isExisted = false;

               SPQuery query = new SPQuery();

               query.Query = string.Format(

                   "<Where><Eq><FieldRef Name='FileLeafRef'></FieldRef><Value Type='Text'>{0}</Value></Eq></Where>",

                   fileName);

               query.ViewAttributes = "Scope=\"Recursive\"";

               SPListItemCollection itemColl = list.GetItems(query);

               if (itemColl.Count > 0)

               {

                   isExisted = true;

               }

               return isExisted;

           }

     

    Hope it’s helpful.

    Wednesday, July 21, 2010 3:43 AM

All replies

  • Hi Raju T,

     

    Thanks for your post.

     

    There are some different from developing Event handler in sharepoint 2007, so I suggest you to read the reference at first:

    http://philwicklund.com/archive/2009/10/21/building-an-event-handler-and-custom-error-page-in-sharepoint-2010.aspx

     

    Then I have written some sample code for this issue:

    /// <summary>

           /// An item is being added.

           /// </summary>

           public override void ItemAdding(SPItemEventProperties properties)

           {

               string fileUrl = properties.BeforeUrl;

               string fileName = fileUrl.Substring(fileUrl.LastIndexOf('/') + 1);

               if (IsExisted(properties.List, fileName))

               {

                     properties.Cancel = true;

                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

                   properties.RedirectUrl = "/_Layouts/MyEventError.aspx";

     

     

               }

           }

     

           /// <summary>

           /// An item is being updated.

           /// </summary>

           public override void ItemUpdating(SPItemEventProperties properties)

           {

               string fileUrl = properties.BeforeUrl;

               string fileName = fileUrl.Substring(fileUrl.LastIndexOf('/') + 1);

               if (IsExisted(properties.List, fileName))

               {

                   properties.Cancel = true;

                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

                   properties.RedirectUrl = "/_Layouts/MyEventError.aspx";

               }

           }

     

           private Boolean IsExisted(SPList list, string fileName)

           {

               bool isExisted = false;

               SPQuery query = new SPQuery();

               query.Query = string.Format(

                   "<Where><Eq><FieldRef Name='FileLeafRef'></FieldRef><Value Type='Text'>{0}</Value></Eq></Where>",

                   fileName);

               query.ViewAttributes = "Scope=\"Recursive\"";

               SPListItemCollection itemColl = list.GetItems(query);

               if (itemColl.Count > 0)

               {

                   isExisted = true;

               }

               return isExisted;

           }

     

    Hope it’s helpful.

    Wednesday, July 21, 2010 3:43 AM
  • Hi Wayne,

    Thanks for giving code. the code is working fine and it shows errror when uploading same documents. I say thanks to u.

    and also i need u r help  on  when i  upload same file and i will cheked  the overwrite checkbox in sharepoint , i want to be upload the file in DocLib.

    But in ur above code is not working for upload,  when i checked the overwrite option . pls provide that code also for me

    Sorry for late reply

     

    Wednesday, July 28, 2010 11:51 AM