cancel
Showing results for 
Search instead for 
Did you mean: 
Workflow Hero

Delete duplicate list items

Jump to solution

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?

Labels: (1)
Tags (1)
0 Kudos
Reply
25 Replies
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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.

0 Kudos
Reply
Not applicable

Re: Delete duplicate list items

Jump to solution

you can create a new calculated field with formula (=[Created]) and enforce unique value on this column.

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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.

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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.

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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.

0 Kudos
Reply
Workflow Hero

Re: Delete duplicate list items

Jump to solution

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"

0 Kudos
Reply