evoke it logo white with clear backContact

How to Change Fields on an InfoPath form using CSOM

How to Change Fields on an InfoPath form using CSOM

Most of the time, InfoPath forms are filled in by the end-user or fields within the form are populated by using rules, but what if you needed to update or change a fields value on a regular basis which may prove difficult with using rules in InfoPath. One way of achieving this is by using the Client Side Object Model (CSOM). I will show you how to update fields in an InfoPath Form using CSOM can be achieved.

First of all you need to be familiar with the basic operations of using CSOM with SharePoint. Please see the link below for basic operations through the Client Side Object Model:

https://msdn.microsoft.com/en-us/library/office/fp179912(v=office.15).aspx

In our example we have a SharePoint site and within this site we have a form library called ‘Clients’. These forms contain basic information for each Client in the site such as name, address phone number, client reference etc. Now we have a number of clients whose client reference numbers need to be changed. Now if there were only a few then you could go and manually change them, but what if there were thousands of clients? You would be there for a very long time.First of all we need to connect to the site and connect to the form library we will be using by using the client context. We then write a CAML query to return all the clients in question who’s client reference numbers need to be changed (similar examples can be found using the link above or see below).

So in our above example we have retrieved all items where the file name is not named ‘BLANK.xml’ and the Client Reference is equal to ‘2’.If we have any results then for each client in our results we are going to set the Client Reference to a value of ‘4’ for demonstration.Now because InfoPath is XML based. We first of all we declare a variable which is an XML Document:

We will also need the file ref of the file. This is the relative server URL of the file. Now because we are looping through each item in our results we can grab the file ref for that item similarly below:

 

We can then load the file using the client context and our file ref:

We then need to load the XML using a Stream:

Next we need to get the nodes which we are needing. In our example we are only needing one which is the client reference number:

As you can see above the name of the node is normally “my:” followed by the name.We then loop through each node until we locate the client reference number node and we then set the inner text of the node to our new value. For demonstration we will set it to “4”.

We then create a variable of type ‘byte[]’ which is used to store our new XML:

Finally we have to write our new XML back to the file and overwrite the existing one currently in SharePoint:Arguments passed in ‘SaveBinaryDirect()’ are as follows:

• Client context• Server Relative URL of the file• The memory stream• Overwrite if exists (Boolean)

When we run this application it will:

• Locate all the clients from our query.• Load the file using the client context and server relative URL of the file.• Load our XML using a Stream.• Locate the ‘Client Reference’ node, when it locates the node we set it to a new value.• It then writes our new XML to a byte [].• It then saves our new XML using a memory stream which includes the client context, server relative URL of the file, the memory stream and overwrites the existing file.

You can use the above code to suit the needs for what you require. Hopefully this will make updating fields in an InfoPath form using CSOM more understandable for anyone trying to achieve a similar result.

Be the First to Get Our Ebook
Microsoft SharePoint 2019

What you need to know!

Back To The BlogContact Support

View Post's Via Catagory

Terms & ConditionsPrivacy PolicyDeveloped by Evoke IT