VB.NET InfoPath

G

Guest

Hello

I've tried in the InfoPath groups but no luck yet. Here is my situation:

All computers are on an Active Directory domain in the company. I have an
Infopath form stored on the network (in SharePoint) that needs to be able to
update a SharePoint list when a button is clicked.

I got the sample code from a book called "Developing Solutions with
Microsoft InfoPath".

It seems, from the build errors that I'm not able to get the code to run on
the server because of security.

Here is my original VB.NET code and the errors associated with it:

Imports System
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports System.Xml


' Microsoft Office integration attribute. Identifies the startup class for
the form. Do not modify.
<Assembly: System.ComponentModel.DescriptionAttribute("InfoPathStartupClass,
Version=1.0, Class=iesr.FormCode")>

Namespace iesr
' The namespace prefixes defined in this attribute must remain
synchronized with
' those in the form definition file (.xsf).

<InfoPathNamespace("xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:dfs=""http://schemas.microsoft.com/office/infopath/2003/dataFormSolution""
xmlns:s0=""http://schemas.microsoft.com/sharepoint/soap/""
xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-02-12T15:28:41""
xmlns:xd=""http://schemas.microsoft.com/office/infopath/2003""")> _
Public Class FormCode

Private thisXDocument As XDocument
Private thisApplication As Application

Public Sub _Startup(ByVal app As Application, ByVal doc As XDocument)
thisXDocument = doc
thisApplication = app

' You can add additional initialization code here.
End Sub

Public Sub _Shutdown()
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL7_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL7_17_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL122_5",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL122_5_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub


' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL27_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL27_17_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(EventType:=InfoPathEventType.OnSubmitRequest)> _
Public Sub FormEvents_OnSubmitRequest(ByVal e As DocReturnEvent)
' If the submit operation is successful, set
' e.ReturnStatus = True

End Sub
Public Function CreateTask(ByVal strFull_x0020_Name As String, _
ByVal strPreferred_x0020_First_x0020_Name
As String) As Integer

Dim listName As String
Dim listsservice As lists.Lists
Dim node As XmlDocument
Dim res As XmlNode
Dim resultError As XmlNode
Dim resultId As XmlNode

listName = "{AA6DF9CC-4CDF-45F0-9BA2-1F4E2521C537}"

listsservice = New lists.Lists
listsservice.Credentials =
System.Net.CredentialCache.DefaultCredentials

node = New XmlDocument

node.LoadXml("" _
+ "<Batch OnError='Continue' ListVersion='1' xmlns=''>" _
+ " <Method ID='1' Cmd='New'>" _
+ " <Field Name='Full_x0020_Name'>" + strFull_x0020_Name +
"</Field>" _
+ " <Field Name='Preferred_x0020_First_x0020_Name'>" +
strPreferred_x0020_First_x0020_Name + "</Field>" _
+ " </Method>" _
+ "</Batch>")

res = listsservice.UpdateListItems(listName, node)

Dim nsmgr As XmlNamespaceManager
nsmgr = New XmlNamespaceManager(res.OwnerDocument.NameTable)
nsmgr.AddNamespace("s",
"http://schemas.microsoft.com/sharepoint/soap/")
nsmgr.AddNamespace("z", "#RowsetSchema")

resultError =
res.SelectSingleNode("s:Result[@ID='1,New']/s:ErrorCode", _
nsmgr)
If (resultError Is Nothing Or _
Convert.ToInt32(resultError.InnerText, 16) <> 0) Then
Return 0
End If

resultId =
res.SelectSingleNode("s:Result[@ID=""1,New""]/z:row/@ows_ID", _
nsmgr)
Return Integer.Parse(resultId.InnerText)
End Function

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL28_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL28_17_OnClick(ByVal e As DocActionEvent)
Dim id As Integer
id = CreateTask( _

thisXDocument.DOM.selectSingleNode("/my:myFields/my:NewEmployeeSection/my:Full_x0020_Name").text, _

thisXDocument.DOM.selectSingleNode("/my:myFields/my:NewEmployeeSection/my:preferred_x0020_First_x0020_Name").text)

If (id = 0) Then
thisXDocument.UI.Alert("Failed to create new Employee.")
Else
thisXDocument.UI.Alert("Created new Employee ID " +
id.ToString() + ".")
End If
End Sub
End Class
End Namespace


My data connection is called Lists and is pointing to the SharePoint
lists.asmx?wsdl Web service.

When running the code from Visual Studio 2005 it stops on this line: res =
listsservice.UpdateListItems(listName, node)

And has this error: Request for the permission of type
'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.

If I run the form from its saved location in SharePoint, I get this error
after I click the button on the form that is setup to create the new task in
the SharePoint list:

Request for the permission of type
'System.Security.Permissions.EnvironmentPermission, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.CredentialCache.get_DefaultCredentials()
at iesr.FormCode.CreateTask(String strFull_x0020_Name, String
strPreferred_x0020_First_x0020_Name)
at iesr.FormCode.CTRL28_17_OnClick(DocActionEvent e)
at
Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)



Here is my current VB.NET code and the build errors associated with it:

Imports System
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports System.Xml
Imports System.Net


' Microsoft Office integration attribute. Identifies the startup class for
the form. Do not modify.
<Assembly: System.ComponentModel.DescriptionAttribute("InfoPathStartupClass,
Version=1.0, Class=iesr.FormCode")>

Namespace iesr
' The namespace prefixes defined in this attribute must remain
synchronized with
' those in the form definition file (.xsf).

<InfoPathNamespace("xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:dfs=""http://schemas.microsoft.com/office/infopath/2003/dataFormSolution""
xmlns:s0=""http://schemas.microsoft.com/sharepoint/soap/""
xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-02-12T15:28:41""
xmlns:xd=""http://schemas.microsoft.com/office/infopath/2003""")> _
Public Class FormCode

Private thisXDocument As XDocument
Private thisApplication As Application

Public Sub _Startup(ByVal app As Application, ByVal doc As XDocument)
thisXDocument = doc
thisApplication = app

' You can add additional initialization code here.
End Sub

Public Sub _Shutdown()
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL7_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL7_17_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL122_5",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL122_5_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub


' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL27_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL27_17_OnClick(ByVal e As DocActionEvent)
' Write your code here.
End Sub

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(EventType:=InfoPathEventType.OnSubmitRequest)> _
Public Sub FormEvents_OnSubmitRequest(ByVal e As DocReturnEvent)
' If the submit operation is successful, set
' e.ReturnStatus = True

End Sub
Public Function CreateTask(ByVal strFull_x0020_Name As String, _
ByVal strPreferred_x0020_First_x0020_Name
As String) As Integer

Dim listName As String
Dim listsservice As lists.Lists
Dim node As XmlDocument
Dim res As XmlNode
Dim resultError As XmlNode
Dim resultId As XmlNode


listName = "{AA6DF9CC-4CDF-45F0-9BA2-1F4E2521C537}"

listsservice = New lists.Lists
'listsservice.Credentials =
System.Net.CredentialCache.DefaultCredentials
'CredentialCache cache = new CredentialCache();
'cache.Add( listUrl, "NTLM", new NetworkCredential(username,
password) );
'listsservice.Credentials = cache;

listsservice = New lists.Lists
listsservice.PreAuthenticate = True
CredentialCache(Cache = New CredentialCache)
Cache.Add(New Uri(listsservice.Url), "NTLM", New
NetworkCredential("username", "password"))
listsservice.Credentials = Cache

node = New XmlDocument

node.LoadXml("" _
+ "<Batch OnError='Continue' ListVersion='1' xmlns=''>" _
+ " <Method ID='1' Cmd='New'>" _
+ " <Field Name='Full_x0020_Name'>" + strFull_x0020_Name +
"</Field>" _
+ " <Field Name='Preferred_x0020_First_x0020_Name'>" +
strPreferred_x0020_First_x0020_Name + "</Field>" _
+ " </Method>" _
+ "</Batch>")

res = listsservice.UpdateListItems(listName, node)

Dim nsmgr As XmlNamespaceManager
nsmgr = New XmlNamespaceManager(res.OwnerDocument.NameTable)
nsmgr.AddNamespace("s",
"http://schemas.microsoft.com/sharepoint/soap/")
nsmgr.AddNamespace("z", "#RowsetSchema")

resultError =
res.SelectSingleNode("s:Result[@ID='1,New']/s:ErrorCode", _
nsmgr)
If (resultError Is Nothing Or _
Convert.ToInt32(resultError.InnerText, 16) <> 0) Then
Return 0
End If

resultId =
res.SelectSingleNode("s:Result[@ID=""1,New""]/z:row/@ows_ID", _
nsmgr)
Return Integer.Parse(resultId.InnerText)
End Function

' The following function handler is created by Microsoft Office
InfoPath. Do not
' modify the type or number of arguments.
<InfoPathEventHandler(MatchPath:="CTRL28_17",
EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL28_17_OnClick(ByVal e As DocActionEvent)
Dim id As Integer
id = CreateTask( _

thisXDocument.DOM.selectSingleNode("/my:myFields/my:NewEmployeeSection/my:Full_x0020_Name").text, _

thisXDocument.DOM.selectSingleNode("/my:myFields/my:NewEmployeeSection/my:preferred_x0020_First_x0020_Name").text)

If (id = 0) Then
thisXDocument.UI.Alert("Failed to create new Employee.")
Else
thisXDocument.UI.Alert("Created new Employee ID " +
id.ToString() + ".")
End If
End Sub
End Class
End Namespace


I have the following errors:

1. 'CredentialCache' is a type and cannot be used as an expression
2. 'System.Net.Cache' is a namespace and cannot be used as an expression
3. 'Add' is not a member of 'Cache'
4. 'System.Net.Cache' is a namespace and cannot be used as an expression

Any help would be greatly appreciated! Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top