Infopath Level 2: Add vsta hosting code to infopath and query data from the Sharepoint list

Source: Internet
Author: User
Tags hosting

Abstract:In this section, we will learn how to addCodeAnd then how to publish a form template containing code to a website with forms service. This code is relatively simple: according to the example in the previous chapter, the template contains a field called"Applicant", This code is used to query his department based on the name of the current logon user, and then automatically fill in"Application Department. In this way, when the user creates this form for the first time, the applicant and department will automatically fill in the Form, which makes the template more intelligent.

You will learn:

    1. How to add code to the infopath template and publish it.
    2. How to read the Chinese name of a user from the moss user configuration file
    3. How to Use the OM and query statements to query the Sharepoint list
    4. HowProgramSet the value of a node (field) in the form
    5. How to Apply loading events

 

1. Create a new form library to store forms

For example:

 

2. Edit the form template and add the program

1. Open the template in design mode and set it in "Tools"-"form options ".Programming LanguageC #, for example:

 

2. Set the security level of the form template to "full trust" and form template signature, for example:

3. Open the programming interface through the loading event in the menu:

 

4. Add three necessary "references" to the program ",To open the "add reference" dialog box,

1) Add a WSS reference to the Assembly, mainly using the OM object model, such:

2) add an office server reference to the Assembly, mainly using the configuration file class, such:

3) refer to the previous example and add the system. Web reference to the program, which is mainly used to use the servercontext object.

 

5. Add code to the program

1) Add a reference line to the program, as shown in the following example:

// Manually add the followingUsingSystem. Web;UsingMicrosoft. Office. server;UsingMicrosoft. Office. server. administration;UsingMicrosoft. Office. server. userprofiles;UsingMicrosoft. SharePoint;

 

 

 

 

 

 

 

2) Add the following code to the loading event:

             If ( This . New) // If it is a new form, set the initial value. {// Obtain the fullname value of the current user in the configuration file                  Using (Spsite site = New Spsite (" Http: // OSS ")) // Replace this value with the URL of your SharePoint website { // The following configuration files are obtained based on the current user login name: Servercontext context = servercontext. getcontext (SITE); userprofilemanager profilemanager = New Userprofilemanager (context); USERPROFILE u = profilemanager. getuserprofile ( This . Application. User. loginname );String Fullname = U [propertyconstants. preferredname]. value. tostring (); xpathnavigator mainnavigator = This . Maindatasource. createnavigator (); // Retrieve the cursor of the primary data source                      // Set the applicant's node value to the user name, that is, the preferredname attribute in the configuration file Mainnavigator. selectsinglenode (" /My: myfields/My: Applicant ", This . Namespacemanager). setvalue (fullname ); // Query a department from the SharePoint department list by name                      Using (Spweb = site. allwebs [" /Demo "]) // Replace this with the URL of your SharePoint website {Spquery query = New Spquery (); // Because the name field is changed from the title, its internal name is Title Query. query =" <Where> <EQ> <fieldref name = 'title'/> <value type = 'text'> "+ Fullname +" </Value> </EQ> </where> "; Splist list = web. Lists [" Department staff "]; Splistitemcollection items = List. getitems (query ); If (Items. Count> 0)// Set the department value if you find it. Mainnavigator. selectsinglenode (" /My: myfields/My: Application Department ", This . Namespacemanager). setvalue (items [0] [" Department "]. Tostring ());}}}}
 
 
 
 

6. Check the debugging result.

Please refer to the following debugging results, for example:

 

3. Deploy the template to the document library

After all the programs have been debugged, they should be deployed in the document library. How should we deploy them?

1. StartFile menuInReleaseWizard to publish the template to the root folder of the C: disk, for example:

 

 

2. Upload the template just released to the management center and activate it to the website set.

Go to the "Application Management" page in the Management Center. Click to enter the upload page:

 

 

Click Upload and click OK to go to the template list page. For example, activate the template to the website set:

On the configuration page, configure the website set to use this template:

3. Set the document library

1) first open the"Procurement application"Document LibraryAdvanced Settings,OpenContent type management,And configuredWebpage displayThis form library is shown in the following figure:

 

2) Add"Purchase Application Form"Content type:

 

Add the purchase application form to the right

Result

 

 

 

 

 

 

 

 

 

 

 

 

 

3) set the content type to a unique template and delete the original form template,

Click"Change the order and default content type of the 'new' button",

 

SetPurchase application form,Set to 1st content type, for example:

 

Click to enterFormOn the content type setting page, click Delete:

 

Iv. Test Results

For example, click Create

 

The result is as follows:

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.