Hiển thị các bài đăng có nhãn InfoPath. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn InfoPath. Hiển thị tất cả bài đăng

Thứ Hai, 29 tháng 3, 2010

Cascading Drop-Down List in SharePoint 2010 using InfoPath 2010

Cascading drop-down is a common business requirement. SharePoint 2007 does not support cascading drop-down list by default. User have to write custom SharePoint field and it’s not easy to implement. With SharePoint 2010, User able to customize SharePoint form in easy way by using the powerful of InfoPath Designer. Following is the step guides to create a cascading drop-down list solution in SharePoint 2010 without require a lot of IT knowledge.

Scenario: We build a cascading for Address List. We have three list: City, District and Ward

1. Create City List

- In SharePoint 2010, create a custom List and named as “City”

- Add Two Item to this list: Hanoi and TP HCM

2. Create District List

- In SharePoint 2010, create a custom List and named as “District”

- Create a Lookup column named as “City”. This column get information from City List in the column Title.

- Add 4 items to District list

A21319952F68E79E_694_0[1]

3. Create Ward List

- In SharePoint 2010, create a custom List and named as “Ward”.

- Create a Lookup column named as “City”. This column get information from City List in the column Title.

- Create a Lookup column named as “District”. This column get information from District List in the column Title.

- Create on Add new Item link you will see. When we choose City, District still have 4 item.

A21319952F68E79E_694_1[1]

4. Customize Ward form to meet cascading requirement

- In Ward List, click on Customize Form ribbon button

A21319952F68E79E_694_2[1]

- System will automatically open the form template of Ward list in InfoPath Designer

A21319952F68E79E_694_3[1]

- Right click on District drop-down list control then select Drop-Down List box Properties from menu to open the properties windows

A21319952F68E79E_694_4[1]

- In Data Source. Click on Add button

- In Data Connection Wizard, click on Next button

A21319952F68E79E_694_5[1]

- In Select the source of your data, choose SharePoint library of list then click Next button

A21319952F68E79E_694_6[1]

- In SharePoint Site details screen, click on Next button

A21319952F68E79E_694_7[1]

- In Select a list of library, choose District the click on Next button

A21319952F68E79E_694_8[1]

- In select fields: Choose Title, City, ID then click on Next button

A21319952F68E79E_694_9[1]

- In next screen, choose Next button

A21319952F68E79E_694_10[1]

- Leave the default name for the connection as “District1” then click Finish button

A21319952F68E79E_694_11[1]

- Now, You will see the Data Source of Drop-Down list box is District1

A21319952F68E79E_694_12[1]

- In Entries section, click on the tree button to select XPath

- In Select a Field or Group dialog, click on Filter Data… button

A21319952F68E79E_694_13[1]

- In Filter Data click on Add… button

A21319952F68E79E_694_14[1]

- In specify Filter Conditions, in the first drop-down list choose Select a field or group

A21319952F68E79E_694_15[1]

- Select a Field or Group opened. Select District1 as Data Source, choose City in dataFields then click OK

A21319952F68E79E_694_16[1]

- In specify Filter Conditions, in the last drop-down list choose Select a field or group

A21319952F68E79E_694_17[1]

- Select a Field or Group opened. Select Main as Data Source then choose City in dataFields then click OK

A21319952F68E79E_694_18[1]

- Click on OK to close specify Filter Conditions dialog

- Click on OK to close Filter Data dialog

- Click on OK to close Select a Field or Group

- In Drop Down List box properties, choose d:ID in Value: section then click OK to close this

A21319952F68E79E_694_19[1]

- In InfoPath Designer, close the Windows then choose the Save and Publish button

A21319952F68E79E_694_20[1]

- After Publishing complete, back to Ward list then click on Add new item link. You will see the result

A21319952F68E79E_694_21[1] A21319952F68E79E_694_22[1]

Two drop-down list has been cascading.

Conclusion

SharePoint 2010 provides a easy way for none IT to custom the look and feel of SharePoint by using InfoPath Designer, SharePoint Designer. Customize form in InfoPath Designer 2010 is a great new feature of SharePoint 2010. It’s cool!

Thứ Năm, 20 tháng 8, 2009

InfoPath: Duplicate Attribute error

“The formula contains one or more errors.
Duplicate attribute”

I got it when trying to add ,xsl as Data Connections to InfoPath then add new Rules to controls. Following this guide. It’s resolved the problem

http://www.softobject.com/sites/Blog/Lists/Posts/Post.aspx?ID=4

“

DUPLICATE ATTRIBUTE ERROR:

This happens when a XSL file is added to the form as 2ndary data source.  The namespace in the XSL conflicts what's in Form's ns. 

RESOLUTION:

Do not add XSL as 2ndary data source.  Instead add it as form's Resource File.  Change your code that might be doing the XSLT transform to load the XSL manually.  This fixes it as charm!

Work around code instead of havinf XSL as a secondary data source

WITH XSL AS SECONDARY DATA SOURCE, YOU COULD HAVE DONE SOMETHING LIKE THIS:

custom.HTMLDocument.body.innerHTML = thisXDocument.DOM.transformNode(thisXDocument.GetDOM("SummaryReport"));


NOW SINCE THAT CAUSES NAMESPACE CONFUSION/CONFLICT, DO THE XSLT LIKE THIS:






IXMLDOMDocument XSLTFileDOM = thisXDocument.CreateDOM(); 
XSLTFileDOM.async = false; 
XSLTFileDOM.validateOnParse = false; 
XSLTFileDOM.load("SummaryReport.xslt"); 
custom.HTMLDocument.body.innerHTML = thisXDocument.DOM.transformNode(XSLTFileDOM);



-DANIEL



“



It work for JScript. But C#, I don’t get the solution yet.



Comments








Quang Nguyen Ba - 8/21/2009 8:52:30 AM
After asking someone over Internet. I found the solution for it. Following the guide from Jimmy http://www.infopathdev.com/forums/p/1641/46072.aspx, It work well. Here are complete codes for implementing transformation from from Resource Files






   1: XslCompiledTransform trans = new XslCompiledTransform();
   2: Stream xslTemplate = this.Template.OpenFileFromPackage("DNTN_Temp.xsl");
   3: XmlReader xslTemplateReader = XmlReader.Create(xslTemplate);
   4: trans.Load(xslTemplateReader);
   5: XmlTextWriter myWriter = new XmlTextWriter(resumeFile + "\\" + filename, null);
   6: trans.Transform(this.MainDataSource.CreateNavigator(), null, myWriter);
   7: myWriter.Close();

Thứ Năm, 13 tháng 8, 2009

Export InfoPath form to Word 2003 document sử dụng XSLT, XPath và C#

Trong bài viết trước, Export InfoPath form to Word 2003 document sử dụng XSLT và XPath tôi đã đã hướng dẫn các bạn cách Export InfoPath form sử dụng JScript code. Bài này hướng dẫn các bạn một cách khác, sử dụng C# Code.

Chúng ta cũng thực hiện các bước tương tự như bài trước. Tuy nhiên lựa chọn Programming language ở đây là C#. Chúng ta sử dụng đoạn code sau cho Button Event

   1: //Retrieve the xsl to use to transform the InfoPath form into document.xml
   2: DataSource ds = this.DataSources["XMLToWordML"];
   3: XslCompiledTransform trans = new XslCompiledTransform();
   4: trans.Load(ds.CreateNavigator());
   5:  
   6: //create the output stream
   7: XmlTextWriter myWriter = new XmlTextWriter
   8:    ("result.xml", null);
   9:  
  10: // Transform the InfoPath form and save the XML into the stream for the part
  11: trans.Transform(this.MainDataSource.CreateNavigator(), null, myWriter);
  12: myWriter.Close() ; 


Các bạn sẽ thấy, kết quả thực hiện không khác so với sử dụng JScript. Bởi vì khi can thiệp vào các Event, Action trong InfoPath, chúng ta không thể sử dụng đồng thời cả JScript và C# cùng một lúc. Do đó, các bạn cần phải lựa chọn Programming Language cho InfoPath. Đối với các bài toàn phức tạp, chúng ta nên sử dụng C#, ngược lại các yêu cầu đơn giản chúng ta sử dụng JScript là một thuận lợi

Thứ Tư, 12 tháng 8, 2009

InfoPath: Custom save form using JScript

Đôi khi chúng ta cần save InfoPath file dưới một tên và folder đã được định sẵn. Bài viết này hướng dẫn các ban làm điều đó.

1. Mở InfoPath để thiết kế 1 form với một textbox control

2. Vào Tools –> Form Options và chọn “Save using custom code” và click on “Edit…” button

   1: function XDocument::OnSaveRequest(eventObj)
   2: {
   3:     try
   4:      {
   5:         //XDocument.UI.SetSaveAsDialogLocation("\\my\form\directory")
   6:         XDocument.UI.SetSaveAsDialogLocation("C:\\")
   7:         var strFileName = XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text + ".xml"; 
   8:         XDocument.UI.SetSaveAsDialogFileName(strFileName);
   9:         eventObj.IsCancelled = eventObj.PerformSaveOperation();
  10:         eventObj.ReturnStatus = true;
  11:      } 
  12:      catch(e)
  13:      {
  14:         XDocument.UI.Alert("Error at OnSaveRequest: " + e.message);
  15:         eventObj.ReturnStatus = false;
  16:      }  
  17: }

3. Save Infopath form. Chúng ta có thể sẵn sàng test.


Comments




Dũng Nguyễn - 11/3/2009 9:21:05 AM
Anh Quang ơi cái này đâu có hỗ trợ cho browser forms đâu. Anh có đoạn code nào về save file infopath hỗ trợ browser forms không ạ.


Bư béo ™ - 2/23/2010 10:55:02 AM
anh ơi em ko hiểu, em mà sử dụng code thì nó ko hỗ trợ browser form:(. anh ơi có cách nào giúp em với ko ạ?


Quang Nguyen Ba - 2/27/2010 4:42:29 PM
Với browser form thì thường chúng ta sẽ save vào SharePoint List. Bài viết này áp dụng cho Offline form. Mục tiêu là giải quyết vấn đề tự động Save với các form offline mà không cần chọn Save Target folder. Như vậy việc nhập dữ liệu hàng loạt sẽ nhanh hơn.
Với code này, anh chưa thử với Browser Form, tuy nhiên anh tinh là sẽ sử dụng được. Tuy nhiên khác một điều là nó sẽ save lên thư mục trên Server deploy InfoPath Service


Quang Nguyen Ba - 2/27/2010 4:44:06 PM
Để dùng được code với Browser Form thì chúng ta cần deploy InfoPath dưới dạng Trusted. Nếu deploy lên MOSS 2007 thì deployment phải được quản lý bởi Central Administration

InfoPath Auto Submit to Local Folder using JScript

Khi sử dụng InfoPath, đôi khi chúng ta có nhu cầu tự động submit với filename là một field nào đó trên InfoPath form, hoặc là được đặt sẵn. Để thực hiện được điều này chúng ta có thể sử dụng C# code hoặc JScript. Bài viết này hướng dẫn các bạn thực hiện điều đó. Bài viết đề cập đến các kỹ thuật sau:

- Tự động submit infopath form sử dụng JScript code.

- Tự động save filename sử dụng một text field trên form

- Kiểm tra folder đã tồn tại hay chưa trước khi submit

- Sau khi submit mở luôn một form mới để nhập

1. Trước hết chúng ta kiểm tra Programming Language default của InfoPath form khi thiết kế sử dụng InfoPath thiết lập cho JScript

A21319952F68E79E_511_0[1]

2. Chúng ta thiết kế một form đơn giản bao gồm một textbox và một button

A21319952F68E79E_511_1[1]

3. Click chuột vào Submit button và chọn Action là Submit. Sau đó click on Submit Option… button.

4. Trong Submit Options dialog box, chọn “Allow users to submit this form” –> Chọn “Perform custom action using Code”. Trong “After submit” option, chọn “Create a new, blank form”. Sau đó click on “Edit Code…” button.

A21319952F68E79E_511_2[1]

5. Chúng ta sử dụng JScript Code như sau để submit

   1: function XDocument::OnSubmitRequest(eventObj)
   2: {    
   3:     //Get the filename
   4:     var strFileName = XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text + ".xml"; 
   5:     var xmlDoc = XDocument.DOM.xml;  
   6:     //create FSO Object
   7:     var objFSO = new ActiveXObject("Scripting.FileSystemObject");  
   8:     //Check the folder exist
   9:     if(!objFSO.FolderExists("C:\\InfoPath"))
  10:   {
  11:   //Create folder
  12:    objFSO.CreateFolder("C:\\InfoPath");
  13:   }            
  14:     //Create file
  15:     var tempFile = objFSO.CreateTextFile("C:\\InfoPath\\" + strFileName , true);  
  16:     //Save
  17:     tempFile.Write(xmlDoc);     
  18:     eventObj.ReturnStatus = true;
  19: }

6. Đảm bảo form được full trust


A21319952F68E79E_511_3[1] 


7. Giờ các bạn sẵn sàng test form.


Note: Nếu chúng ta muốn lưu data sau khi submit vào thư mực hiện thời của .xsn file, chúng ta sử dụng câu lệnh:


var folder = fso.GetFolder(".");


để get current folder của .xsn file.


var folderPath = fso.GetFolder(".").path;


để lấy đường dẫn của thư mục hiện thời

Thứ Hai, 27 tháng 7, 2009

Convert an InfoPath 2007 form into a Word 2007 document using XSLT and C#

Tham khảo bài viết Convert an InfoPath 2007 form into a Word 2007 document using XSLT and C# để thực hiện việc Export InfoPath 2007 form data ra Word 2007. Chúng ta cần chú ý một số điểm sau:

- namespace của xsd phải có đủ trong xsl

- namespace trong w:document có đủ trong xsl:stylesheet của xsl

  • Open Microsoft Visual Studio 2005 and create a new InfoPath C# project.
  • Design an InfoPath form as shown in figure 1.

    Figure 1. InfoPath form in design mode.
  • The Main data source of the InfoPath form should resemble the following figure.

    Figure 2. Main data source of InfoPath form.
  • Create a docx file as shown in figure 3 and save it as template.docx. This Word 2007 document will be used as a template file to base your converted InfoPath forms on.

    Figure 3. The Word 2007 document to use as a template for converting InfoPath forms.
    The text ip_firstName, ip_lastName, ip_website, and table_cell_1 are used as placeholders just to make it easier later on to locate the positions in the XSLT stylesheet where to place references to the InfoPath form fields. Note that table_cell_1 is contained within a table. This table will be used to show the items from the Repeating Table on the InfoPath form.
  • Open Windows Explorer and browse to the template.docx file.
  • Copy template.docx, paste it, and rename it to template.zip.
  • Extract the files from the template.zip file.

    Figure 4. Contents of template.zip.
  • Double-click on the word folder to open it.

    Figure 5. Contents of the word folder in template.zip.
  • Open the document.xml file in Notepad and save it as transform.xsl in the same folder where template.docx is located.
  • Replace
       1: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 

    which is located at the beginning of the transform.xsl file with



       1: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       2: <xsl:output method="xml" />
       3: <xsl:template match="/"> 

  • Append the following XSL tags to the end of the transform.xsl file:

       1: </xsl:template>
       2: </xsl:stylesheet> 

  • Switch to Microsoft Visual Studio 2005, go to the Solution Explorer, open the myschema.xsd file to view its code, copy all of the namespaces defined for the form (except for the default and xsd namespaces), and paste them as attributes of the xsl:stylesheet node in the transform.xsl file. The beginning xsl:stylesheet tag should now look something like:

       1: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
       2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       3: xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-12-31T07:11:15" 
       4: xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" 

  • Copy all of the namespaces defined in the w:document node in the transform.xsl file, and paste them as attributes of the xsl:stylesheet node in the transform.xsl file. The beginning xsl:stylesheet tag should now look something like:

       1: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
       2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       3: xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-12-31T07:11:15" 
       4: xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
       5: xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
       6: xmlns:o="urn:schemas-microsoft-com:office:office" 
       7: xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
       8: xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
       9: xmlns:v="urn:schemas-microsoft-com:vml"
      10: xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
      11: xmlns:w10="urn:schemas-microsoft-com:office:word"
      12: xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      13: xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"

  • Search for ip_firstName in the transform.xsl file and replace it with

    <xsl:value-of select="my:myFields/my:firstName" />


  • Search for ip_lastName in the transform.xsl file and replace it with

    <xsl:value-of select="my:myFields/my:lastName" />


  • Search for ip_website in the transform.xsl file and replace it with

    <xsl:value-of select="my:myFields/my:website" />


  • Search for table_cell_1 in the transform.xsl file and replace it with

    <xsl:value-of select="my:name" />


  • Search for the last </w:tblGrid> tag and append the following XSL code

    <xsl:for-each select="my:myFields/my:countries/my:country">


  • Search for the last </w:tr> tag and append the following XSL code

    </xsl:for-each>


  • The final transform.xsl file should now look something like this file.
  • Open the InfoPath form in design mode.
  • Select the Data > Data Connections... menu item.
  • Click on the Add... button on the Data Connections dialog box.
  • Select the Receive data option button and click on the Next > button.
  • Select the XML document option button and click on the Next > button.
  • Click on the Browse... button to navigate to and select the XSL file you created in step 20.
    Tip: Select All Files (*.*) in the Files of type drop-down list box to be able to see the XSLT file.
  • Click on the Next > button after you have selected the file and returned to the Data Connection Wizard dialog box.
  • Select Include the data as a resource file in the form template or template part and click on the Next > button.
  • Make sure that the Automatically retrieve data when form is opened checkbox is selected and then click on the Finish button.
  • Click on the Close button to close the Data Connections dialog box.
  • Go to the Solution Explorer window, click on the References node, and add a reference to the WindowsBase DLL that comes with the Microsoft .NET Framework 3.0.
  • Double-click on the button to open its Properties dialog box.
  • Click on the Edit Form Code... button and add the following code in the Clicked event handler that InfoPath created for you:

       1: // Define variables for the word template to use and file to create
       2: string wordTemplateFilePath = @"C:\InfoPath\ConvertToWord2007\template.docx";
       3: string wordPrintFilePath = @"C:\InfoPath\ConvertToWord2007\wordprint.docx";
       4: // Copy the template to create a new docx file
       5: File.Copy(wordTemplateFilePath, wordPrintFilePath, true);
       6: // Crack open the package
       7: Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite);
       8: // Retrieve the document.xml part of the new docx file
       9: PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));
      10: // Retrieve the xsl to use to transform the InfoPath form into document.xml
      11: DataSource ds = this.DataSources["transform"];
      12: XslCompiledTransform trans = new XslCompiledTransform();
      13: trans.Load(ds.CreateNavigator());
      14: // Create a StreamWriter to be able to write to the stream of the part
      15: using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
      16: {
      17: // Transform the InfoPath form and save the XML into the stream for the part
      18: trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);
      19: // Close the stream of the part
      20: partStream.Close();
      21: }
      22: // Write changes to the package 
      23: packWordPrint.Flush();
      24: // Close the package
      25: packWordPrint.Close();
      26: // Open the new docx file in Word 2007
      27: Process proc = new Process();
      28: proc.EnableRaisingEvents = false;
      29: proc.StartInfo.FileName = "winword";
      30: proc.StartInfo.Arguments = wordPrintFilePath;
      31: proc.Start();

    NOTE: Remember to change the path to the template.docx file as required by your own situation.


  • Add the following using statements to your form's code file:

    using System.IO;
    using System.IO.Packaging;
    using System.Diagnostics;
    using System.Xml.Xsl;


  • Give your InfoPath form Full Trust by going to Tools > Form Options and selecting the Security and Trust tab. And sign your InfoPath form with a digital certificate.
  • Build your InfoPath project.