Delete duplicate list items


Badge +9

I want to query my list based on Created By and I want to delete duplicated items that a person entered into my list. Users re only allowed to enter one item. If they enter in another item n the list I want the second item to delete right away. Can someone help?


23 replies

Userlevel 6
Badge +16

Perhaps you could add a column to your Sharepoint list named AskedByUser (type Person) and "Enforce unique values"

Userlevel 7
Badge +17

Sounds like a doable workflow, when it starts on item creation it will do the query and save any field, like ID into a collection. Then do a collection operation to count the items. Then have a condition to see if greater than 0, if it is, then delete the current item. Doing so by using the delete item action where ID is equal to the common reference to the current item's ID.

 

Forms do not seem to be able to do this for you as of yet. You can add a validation to the save button to lookup items and if the count is greater than one to say the form is not valid and display the message that you already created an item. The problem I see right now is, you cannot filter the lookup by the current user for some reason. If you can get that working, let me know how, I need to use it.

Userlevel 4
Badge +11

Hi,

you can do a workflow that starts on item creation and query for items created by the same user of the new item and it saves the IDs in a collection variable, then you can count the items in the collection and if they are more than one then you can delete the current item (if there is only one item in the collection, it's the first one for that user, otherwise it's not the first one).

Giacomo

Badge +4

Hi Rency,

My preference would be to remove the option to create another item, if one already exists.

If you are open to using JavaScript you can do the following.

  1. On the list view page add a CEWP.
  2. Select the CEWP and 'Edit HTML Source'.
  3. Add the Style and JavaScript below into the CEWP.
  4. Update the 'listTitle' to the name of your list.

Default hide the 'Add New Item' link, this reduces flicker.

<style>

    #Hero-WPQ2 {display: none;}

</style>

User JavaScript to see if user has already created an item in this list.  If not show the 'Add New Item' link.

<script language="javascript" type="text/javascript">

    var listTitle = "NAMEOFYOURLISTHERE", currentUser, list, listItems;

    // Invoke getData function.

    ExecuteOrDelayUntilScriptLoaded(getData, "sp.js");

    function getData() {

        var ctx = new SP.ClientContext.get_current();

        // Get current user.

        this.currentUser = ctx.get_web().get_currentUser();

        ctx.load(this.currentUser);

        // Get the URL for the default new item form.

        this.list = ctx.get_web().get_lists().getByTitle(this.listTitle);

        ctx.load(this.list, "DefaultNewFormUrl");

        // Get the list item collection.

        var caml = new SP.CamlQuery();

        this.listItems = this.list.getItems(caml);

        ctx.load(this.listItems, "Include(Author)");

        ctx.executeQueryAsync(

             Function.createDelegate(this, success),

             Function.createDelegate(this, failure)

        );

    }

    function success(sender, args) {

        if ((this.currentUser) && (this.listItems)) {   

            var userName = this.currentUser.get_title();

            var hasSubmitted = false;

            // Enumerate over the list items.

            var itemsEnumerator = this.listItems.getEnumerator();

            while (itemsEnumerator.moveNext()) {

                var item = itemsEnumerator.get_current();

                // If the list has an item where the Author is the current user...

                if (userName == item.get_item("Author").get_lookupValue()) {

                    // then the current user has voted.

                    hasSubmitted = true;

                    break;

                }

            }

        }

        if(hasSubmitted) { $get("Hero-WPQ2").style.display = "none"; } // Keep add link hidden.

        else { $get("Hero-WPQ2").style.display = "block"; }  // Show add link.

    }

    function failure(sender, args) { alert(args.get_message());    }   

</script>

What this does:

On page load it check if the current accessing user has already submitted an item to the named list.  If so the 'Add New Item' button is hidden.

Hope this helps.

Cheers,

Barry.

Badge +9

Hi Barry

I actually like this idea a lot and applied it, the issue I ran into is that the it hid the Add New Item link once I save the CEWP.  I haven't added anything to the list and it hid the new item link anyway.

Userlevel 7
Badge +17

Make sure the list webpart is on top of the page. Changing the list page changes how the ribbon can work with the contents.

Badge +9

Hi Andrew

I attempted to follow your direction. This is what I have, please tell me what I am doing wrong because I keep running into an error.

143724_pastedImage_0.png

143725_pastedImage_1.png

143729_pastedImage_2.png

143730_pastedImage_3.png

Badge +4

I only have access to test this within SP2010 at the moment.  Did you update the following line to contain the name of your list?

var listTitle = "NAMEOFYOURLISTHERE"

Badge +9

Yes I did enter in the name of the list. I am on SP 2013 I added the CEWP below the list and the new item link did not disappear after I added an item. It still appeared.  I appreciate the help happy.png

Userlevel 7
Badge +17

In the delete item, have it as ID equals (current item ID). It should be a workflow context reference.

Badge +9

Under workflow context I do not see Current Item ID

143731_pastedImage_0.png

Badge +9

I got it working!!! Thank you soooo much Andrew! I had to use List Lookup to find the Current Item ID

143732_pastedImage_0.png

Badge +9

Thank you so much for helping me Fernando!!!

Badge +9

Thank you everyone for trying to help me, you are all the best and I am learning so much from each and every one of you happy.png

Best community ever!

Badge +9

Thank you Glacomo!!!

Badge +9

Thank you Ajay!

Badge +6

FYI, you cannot enforce unique values if you have item level permissions turned on ;( I have to use a workflow because I cannot set my list to have item level permission and enforce unique values.

Badge +6

I attached a exported workflow of this if anyone wants to use it.

Badge +3

Hey Rency,

Thanks for bringing up this question. I have a quick question on this WF. I am able to delete the second item created using the WF but the issue is when ever my colleagues creates item, they are getting deleted. I am not sure what I am missing. Can you please help me with this is?

THanks,

Shreedhar

Badge +6

Try importing this attached workflow. Your "run if" action is probably not set up properly.

Badge +3

Hello Emily,

Thanks for the attachment. Do I have to apply filters to query based on Created By as I want to delete duplicated items created by that person?

Thanks,

Shreedhar

Badge +6

162362_pastedImage_3.png

162361_pastedImage_2.png

Badge +4

Thanks Thulasi for your solution. Could you please suggest me on the following criteria?

I wanted to compare emailID, First Name and Last name fields together what is best method to implement it? java script or Workflow? 

Reply