1

I have built a WCF Application that populates a SP Online list. This is working without any issues.

But, when I run the WCF for a second time, I get doubles of everything. So first of all I want to update the list - Instead of add everything again. The update function is not working, not even just overwriting all records. And I cannot see why.

I am now trying to create a check that will look if the item exists already in the list, and if it does, then it will skip over that and move on to the next item.

Now the question is, how can I compare the two? Probably in the form of a foreach loop but that seems like a lot of overhead and very time consuming.

This is the start of a console application to "play around" in. So far, I know, all it does is return a list of items in the list.

 using (ClientContext clientContext = new ClientContext("https://mydomain.sharepoint.com/"))
        {
            SecureString passWord = new SecureString();
            foreach (char c in "mypassword".ToCharArray()) passWord.AppendChar(c);
            clientContext.Credentials = new SharePointOnlineCredentials("myname@mydomain.onmicrosoft.com", passWord);
            Web web = clientContext.Web;
            SP.List oList = clientContext.Web.Lists.GetByTitle("listTitle");
            clientContext.Load(web);
            clientContext.Load(oList);
            CamlQuery query = CamlQuery.CreateAllItemsQuery();
            SP.ListItemCollection itemcoll = oList.GetItems(query);


            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();

            foreach (ListItem oListItem in itemcoll)
            {
                Console.WriteLine("Title: {0} ", oListItem["Title"]);
                oListItem.Update();            //Added
            }

                clientContext.ExecuteQuery(); ///Added
                Console.ReadLine();

Ok, so where I put the comment "///Added " are new lines that update the list.

The one problem I still have, and I'm not sure if this falls under a new question, is that when the WCF service runs, it does update the item already in the list but then it still adds the item again, so although everything is updated, it is also duplicated

  • I don't know of a built-in way to check if a list item exists or not. First you would need to figure out how you want to determine equality between new and existing list items. Some field with a unique value per list item perhaps. What you could then do is to dynamically built a caml query with an "IN" node containing all these values, and then return these items so you know which to skip. – Cameron Verhelst Mar 18 '14 at 13:50
  • I want to compare the date column, as the info that is getting put into the list gets updated, so basically if it's updated then it should not be regarded as a duplicate. And then use the URL to determine the uniqueness of the post. I'm not so familiar with CAML, but will definitely look into that then, so maybe a foreach loop with a CAML query inside it? Is that possible? – Scubacode Mar 18 '14 at 14:25
  • I wouldn't use the foreach loop with a caml query inside, unless you expect a great amount of items and want to return them in batches or something. – Cameron Verhelst Mar 18 '14 at 14:30
  • that's the thing. The first time this thing runs, it will have to 1000's of items into the list, after that, it will have to provide a check to see of those items, which one's have been updated. I haven't been able to find any suggestions/examples of ways to do it - especially a way that won't use too many resources – Scubacode Mar 18 '14 at 14:40
  • What is more likely, that you will find that the item already exists or that the item doesn't exist ? Depending on this, you can built the query to return the items matching the items you want to insert or not, as long as you keep this amount manageable, you will be fine. a few 1000's is still ok. But keep in mind to put a row limit on the query (don't actually know if it's possible in ClientContext ?) – Cameron Verhelst Mar 18 '14 at 14:43
0

So this is the way, that I ended up doing it. It works, it's been tried and tested and no issues of errors. Caml Query with an ID column for comparing

 using (ClientContext clientContext = new ClientContext("https://mydomain.sharepoint.com/"))
    {
        SecureString passWord = new SecureString();
        foreach (char c in "mypassword".ToCharArray()) passWord.AppendChar(c);
        clientContext.Credentials = new SharePointOnlineCredentials("myname@mydomain.onmicrosoft.com", passWord);
        Web web = clientContext.Web;
        SP.List oList = clientContext.Web.Lists.GetByTitle("listTitle");
        clientContext.Load(web);
        clientContext.Load(oList);
        CamlQuery query = CamlQuery.CreateAllItemsQuery();
        SP.ListItemCollection itemcoll = oList.GetItems(query);


        clientContext.Load(itemcoll);

        clientContext.ExecuteQuery();


        foreach (var oListItem in itemcoll)
        {
            CamlQuery query = new CamlQuery();

            query.ViewXml =          

                    "<View>"
                        + "<Query>"
                            + "<Where>"
                                + "<Eq>"
                                    + "<FieldRef Name='item_ID'/>"
                                         + "<Value Type='Text'>"
                    + oListItem.ID
                                         + "</Value>"
                                + "</Eq>"
                            + "</Where>"
                        + "</Query>"
                     + "</View>";
            ListItemCollection itemcoll = oList.GetItems(query);


            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();



        try
        {

            itemcoll["Title"] = oListItem.Title;             
            itemcoll.Update();
            oList.Update();
        }
        catch(exception e)
        {
        }

        clientContext.ExecuteQuery();

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.