Infopath development experience Section

Source: Internet
Author: User
Reprinted: http://blog.csdn.net/yanwei100/archive/2006/05/16/740766.aspx

Infopath uses XML to store data.

Developers can use the thisxdocument object to access infopath data.

1. To access field data, you must set XPath and namespaces first.

Tempdom = (ixmldomdocument2) thisxdocument. Dom;
Tempdom. setproperty ("selectionlanguage", "XPath ");
Tempdom. setproperty ("selectionnamespaces ",
"Xmlns: DFS = \" + "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" +
"\" Xmlns: My = \ "http://schemas.microsoft.com/office/infopath/2003/myxsd/2006-05-12t03-20-30 \"");
Nodeentry = tempdom.doc umentelement. selectsinglenode ("My: Name ");

2. Modify Field Data

Nodeentry. Text = "123 ";

3. Insert a group node.

Here, we first obtain the first tasks node. Clone the node and insert it. Finally, delete the original tasks node.

Ixmldomdocument2 tempdom;
Ixmldomnode nodeentry;
Tempdom = (ixmldomdocument2) thisxdocument. Dom;
Tempdom. setproperty ("selectionlanguage", "XPath ");
Tempdom. setproperty ("selectionnamespaces ",
"Xmlns: DFS = \" + "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" +
"\" Xmlns: My = \ "http://schemas.microsoft.com/office/infopath/2003/myxsd/2006-05-12t03-20-30 \"");
Nodeentry = tempdom.doc umentelement. selectsinglenode ("My: Tasks ");
Ixmldomnode node;
Node = nodeentry. clonenode (false );

Ixmldomnodelist list = tempdom.doc umentelement. selectnodes ("My: Tasks ");

Try
{Nodeentry. parentnode. appendchild (node );

// Note that if you want to delete the original tasks node, You need to delete it after the new appendchild node. Otherwise, appendchild (node) will fail after all tasks nodes are deleted.
Foreach (ixmldomnode nd in List)
{
Nodeentry. parentnode. removechild (ND );
}

}
Catch (exception ex)
{

}

I encountered another problem during development. If you forget some attributes of the group node, calling appendchild (node) Fails. The solution is to use the insertbefore method instead.

4. dynamically change the web call address

I use a field to save the URL address of WebService to be called, read it at startup, and modify the dataobject URL during the call.

Public void onload (docreturnevent e)
{< br> // write your code here.
ixmldomnode node = e.xdocument.dom.doc umentelement. selectsinglenode ("My: txtwebsiteurl");
This. websiteurl = node. text;
}

Private void applywebserviceurl (dataobject dataobj)
{
Webserviceadapter ADP = dataobj. queryadapter as webserviceadapter;
Xmldocument Doc = new xmldocument ();
Doc. loadxml (ADP. Operation );
Xmlnode node = Doc. selectsinglenode ("// @ serviceurl ");
Node. value = This. websiteurl;
ADP. Operation = Doc. outerxml;
}

5. Call Web Service

// Locate the dataconnect to be called
Dataobject dataobj = (dataobject) thisxdocument. dataobjects ["saveevent"];
If (dataobj = NULL)
{
Thisxdocument. UI. Alert ("the data connect saveevent does not exist .");
Return NULL;
}
// Set the Web Service input parameters. Here, the entire XML data is used as the input parameter.
Ixmldomnode nodeentry;
Try
{
Nodeentry = dataobj. Dom. selectsinglenode ("/DFS: myfields/DFS: queryfields/TNS: saveevent ");
Nodeentry. selectsinglenode ("TNS: Input"). Text = dataobj. Dom. xml;
}
Catch (exception ex)
{
String err = ex. message;
}
// Call the Web Service
This. applywebserviceurl (dataobj );
Dataobj. Query ();
// Obtain the returned value of Web Service, which is an xmldocument, including projectid and eventid.
Ixmldomnode noderesult = dataobj. Dom. selectsinglenode ("/DFS: myfields/DFS: datafields/TNS: saveeventresponse/TNS: saveeventresult ");
String projectid, eventid;
Projectid = noderesult. selectsinglenode ("// projectid"). text;
Eventid = noderesult. selectsinglenode ("// eventid"). text;

6. Set the timeout length of the web service.

My Web Service is sometimes slow. The default time out is 30 seconds, which leads to an exception that occurs frequently when my form is called. The followingCodeSet the timeout length to 120 seconds.

Dataobject dataobj = (dataobject) thisxdocument. dataobjects ["checkallresources"];
Webserviceadapter2 ADP = dataobj. queryadapter as webserviceadapter2;
ADP. Timeout = 120;

7. Use code to control the button status

My infopath needs to control user operations based on the current user's role. Specifically, if the user has the Save permission, the Save button is in the enable State; otherwise, the Save button is set to disable.

Infopath buttons all have the conditional formating function. We can control the display attributes such as the button status or font color based on the conditional formating result. Conditional formating provides simple calculations, such as whether the value of myfield1 is equal to 1 or whether the current user's role is equal to a role.

If these simple calculations cannot meet your needs, you can also use xdextension to call your own code.

1) first define a computing function in your code. Here, my function is to take the bool value of a field and return

Public bool saveenable ()
{
String enable = thisxdocument. Dom. selectsinglenode ("/My: myfields/My: saveenable"). text;
If (Enable. toupper () = "true ")
{
Return true;
}
Else
{
Return false;
}
}

2) define the button's conditional formating,

Right-click the button and choose

Button properties-> display-> Conditional Formatting...-> Add,

Set conditions in the dialog box, select the expression from the drop-down list on the left, and enter:

Xdextension: saveenable ()! = String (true ())

Select disable this control, and then click OK.

8. Blank fields are allowed for processing. Delete the xsi: Nil attribute of the node when writing data to a field that is allowed to be null.

Nodeentry. Attributes. removenameditem ("xsi: Nil ");

 

9. Use Web service to increase startup speed

In my project, infopath 2003 is used as the client. Because my forms have many dropdown lists, it is very slow at startup. After a small adjustment, the startup speed is much faster.

It turns out that each dropdown list data source is an independent data connect that connects to the Web Service of the server. Therefore, during each startup, the dropdown list should be initialized in sequence, and each dropdown list will call the Web service once, while the Web Service is very slow. Slow startup speed.

To speed up, I adjusted data connect to create a web service for all the dropdown lists and added the original results returned for each web service to the root of an XML file, you only need to call it once during the entire startup process.

For example, I originally had two dropdown lists, namely departments and personnel, which correspond to two web services on the server,

Public xmldocument getdocumments (), Public xmldocument getcontacts ().

Now, I add a web service method public xmldocument getinitdata () on the server. The returned value of this method is an XML document, which contains two subnodes and one containing the results of getparts, the other one stores the getcontacts result. Note that the names of each data result set must be unique. If you use the XML directly converted from dataset, make sure that the names of each table are unique.

The most basic but easily overlooked design principle:

1. Reduce interactions

2. Reduce data transmission volume.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.