Code help to update/delete SP List items where checkboxes are selected (ID) value - InfoPath Dev
in

InfoPath Dev

Code help to update/delete SP List items where checkboxes are selected (ID) value

Last post 02-18-2013 05:16 PM by Patrick Halstead. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 02-14-2013 06:10 AM

    Code help to update/delete SP List items where checkboxes are selected (ID) value

     

    0

    I have two code snippets working.

    1. Update SP list item where ID = 5

    string siteUrl = "http://localhost/"; ClientContext clientContext = new ClientContext(siteUrl); List oList = clientContext.Web.Lists.GetByTitle("Inventory"); ListItem oListItem = oList.GetItemById(5); oListItem["Title"] = "Item999"; oListItem.Update(); clientContext.ExecuteQuery();

    2. iterate through selected checkboxes in InfoPath and display in textbox

       // Write your code here.
                XPathNavigator root = MainDataSource.CreateNavigator();
                XPathNodeIterator iter = root.Select("//my:myFields/my:group1", NamespaceManager);
    
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
                        while (iter.MoveNext())
                {
                    string value = iter.Current.Value;
                    sb.Append(value);
                    sb.Append(";");
                }
    
                root.SelectSingleNode("//my:myFields/my:field3", NamespaceManager).SetValue(sb.ToString());

    I need help modifying/combining both of these to accomplish:

    I want to update SPList item where selected checkboxes (ID value) is equal to the items in the list. I think I need to write a CAML and/or spquery but I couldnot get it to work. I think I need to iterate through selections(checkboxes) and create a dynamic CAML string through a loop and use this is my

    spquery. I just am having a hard time choosing the classes or methods that work. I can also achieve this through web services and a batch file but I still need a dynamic CAML query to use through that else well.

    ANy assistance either approach would work , is greatly appreciated. Thanks

  • 02-14-2013 07:53 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    qRules can do this - and prevent you from having to write and maintain custom code.

    That said, inside your loop where you are going through the checkboxes, you could just simply add snippet 1, replacing your hardcoded ID of 5 with your value variable converted to an int.

    Hilary Stoupa

  • 02-14-2013 08:03 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    I understand that much. I just wasn't sure what classes I use. I tried a caml string but it gives me cannot convert implicit string to call query error. I put the string variable where the 5 was. Can you give me an example with the correct syntax? Thanks
  • 02-14-2013 08:11 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    After this line:

    string value = iter.Current.Value;

    You could do something like this:

    int id;
    Int32.TryParse(value, out id);

    Then, when you add snippet 1, you'd use:

    oList.GetItemById(id);

    Hilary Stoupa

  • 02-14-2013 11:28 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    Thanks. I almost there. it works with one selection but when I select multiple items i get the following error. 

    SeverException was handeled by user code. 
    Item does not exist. It may have been deleted by another user. 

    Highlighted in green was clientContext.ExecuteQuery.  I think it had to do with incorrect query with multiple values. I believe my code is formed wrong or wrong syntax.

    here is my code.

    //update list item

    string siteUrl = http://sp-app/dev;
    ClientContext
    clientContext = new ClientContext(siteUrl);

    list oList = clientContext.Web.Lists.GetByTitle("Item Inventory");

    PathNavigator root = MainDataSource.CreateNavigator();

    XPathNodeIterator iter = root.Select("/my:myFields/my:group1", NamespaceManager);

    while (iter.MoveNext())

    {

    string value = iter.Current.Value;

    int id;

    Int32.TryParse(value, out id);

    ListItem oListItem = oList.GetItemById(id);

    oListItem[

    "Title"] = field1;

    oListItem.Update();

    clientContext.ExecuteQuery();

  • 02-14-2013 12:04 PM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    Is this necessary?

    clientContext.ExecuteQuery();

    Isn't the items updated with oListItem.Update() ?

    MSDN says that Update():

    Updates the database with changes that are made to the list item.

    Hilary Stoupa

  • 02-14-2013 01:06 PM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    I tried without the execute but it does nothing I think its because i am using ClientContext class (Microsoft.SharePoint.Client)‎ 

    I even tried this way but

    using (SPSite site = new SPSite(http://sp-app))
    {

    using (SPWeb web = site.OpenWeb())
    SPList list = web.GetList("/lists/item/%20inventory/");
    list.GetItemById(id);
    list.Update();

    but i get this

    The Web application at http://sp-app could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

    when i use power shell Get-SPSite the url is in the list.  

  • 02-16-2013 08:42 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

     Hillary,

     

    I figured out the problem. Its in the iter.move code(XPathNodeIterator iter = root.Select("//my:myFields/my:group1", NamespaceManager);. 

    When  I select more than one checkbox the loop concates the values together instead of using one value and looping through to the next value.  I am still troubleshooting my error.

     

    string siteUrl = "http://localhost/";

    ClientContext clientContext = new ClientContext(siteUrl);

    List oList = clientContext.Web.Lists.GetByTitle("Inventory");

    while (iter.MoveNext())

     

     

    {

     

     

     

    string value = iter.Current.Value;

    ListItem oListItem = oList.GetItemById(value);

     

     

     

     

    oListItem["Title"] = "scuba";

     

     

    oListItem.Update();

     

    clientContext.ExecuteQuery();

     

    }

     

  • 02-17-2013 11:50 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    Once again, I just want to remind you that a common library is the way to go.

    Hardcoding a form template with xpaths in compiled code will guarantee extra costs down the road.

    That's why we developed qRules - to help people out with all of their forms. No developer needed.

    It's xpath agnostic and it helps us support the forum.

    Please consider trying qRules to help us keep putting hours into this forum.

    Yours truly,

    P

    Patrick Halstead
    Project Manager at Qdabra
  • 02-18-2013 05:28 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    I partially agree.  If I envision the need for qRules on many of our forms than that would be the way to go. I just dont see that.  I only have one form that I see will require some extra coding. If I knew  all of the libraries. this would be easy.  All of our other forms can be done without code.
  • 02-18-2013 09:12 AM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    How can this be achieved using q-Rules?
  • 02-18-2013 05:08 PM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    We have a lot of documentation on how to update and delete list items from InfoPath forms

    Also, check out videos here: http://www.qdabra.com/en/products/qRules_learn.aspx 

    • specifically, check out the one about synchronizing forms with SharePoint lists

    If you'd rather see a live demo, let us know. We're happy to arrange a quick 15 minute demo for you.

    Best regards,

    Patrick

    Patrick Halstead
    Project Manager at Qdabra
  • 02-18-2013 05:16 PM In reply to

    Re: Code help to update/delete SP List items where checkboxes are selected (ID) value

    Hi again,

    It's not just a boon for multiple forms. Whenever the form needs to change down the road and you add or update the data source, the qRules approach saves you time since you don't have to recompile your code - just go in and change the affecte rule commands.

    Patrick Halstead
    Project Manager at Qdabra
Page 1 of 1 (13 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.