locked
Can't access SPContext from InfoPath 2007 Web form codebehind RRS feed

  • Question

  • Hi,

    Im trying, for a starter, to do something like this.

    SPWeb web = SPContext.Current.Web;
    SPUser user = web.CurrentUser;
    string s = user.Name;

    The code is running in a InfoPath 2007 Web Form in MOSS 2007. But when the code executes (on a button event) i get the following exception (See below)

    I have tried to set the trust level like this in the virtual directiry web.config (from minimal to medium)

        <trust level="WSS_Medium" originUrl="" />

    That did not work and i still get this: What us wrong?

    -----------------------------------

    Request failed.

    System.Security.SecurityException: Request failed.
    at Microsoft.SharePoint.Administration.SPPersistedObject.ToString()
    at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
    at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite)
    at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
    at Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context)
    at Microsoft.SharePoint.SPContext.get_Current()
    at Ansogning_ver1_2.FormCode.SetupSecurity()
    at Ansogning_ver1_2.FormCode.knap_gemklade_Clicked(Object sender, ClickedEventArgs e)
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.<>c__DisplayClass6.<>c__DisplayClass8.<add_Clicked>b__1()
    at Microsoft.Office.InfoPath.Server.Util.DocumentReliability.InvokeBusinessLogic(Thunk thunk)
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.<>c__DisplayClass6.<add_Clicked>b__0(Object sender, ClickedEventArgs e)
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonEventHost.FireClickedEvent(Object sender, ClickedEventArgs args)
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonFormCode.<>c__DisplayClass2.<>c__DisplayClass5.<Click>b__1()
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.OMExceptionManager.CallFormCodeWithExceptionHandling(UserMessages userMessages, OMCall d)
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonFormCode.<>c__DisplayClass2.<Click>b__0()
    at Microsoft.Office.InfoPath.Server.SolutionLifetime.ButtonFormCode.Click(Document document, XPathNavigator container)
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.EventClick.Play(Document document, BindingServices bindingServices, EventLogProcessor eventLogProcessor)
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.Event.PlayEvent(Document document, BindingServices bindingServices, EventLogProcessor eventLogProcessor)
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.EventLogProcessor.<>c__DisplayClass1.<ExecuteLog>b__0()
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.EventLogProcessor.ExecuteLog(Int32 expectedEventLogID)
    at Microsoft.Office.InfoPath.Server.DocumentLifetime.Document.<>c__DisplayClass6.<PlayEventLog>b__3()
    at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock)
    The action that failed was:
    Demand
    The type of the first permission that failed was:
    Microsoft.SharePoint.Security.SharePointPermission
    The first permission that failed was:
    <IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    version="1"
    ObjectModel="True"/>

    The demand was for:
    <PermissionSet class="System.Security.PermissionSet"
    version="1">
    <IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    version="1"
    ObjectModel="True"/>
    </PermissionSet>

    The only permitted permissions were:
    <PermissionSet class="System.Security.PermissionSet"
    version="1">
    <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Read="USERNAME"/>
    <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Flags="Execution"/>
    <IPermission class="System.Security.Permissions.UrlIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Url="file:///C:/Inetpub/wwwroot/wss/VirtualDirectories/25000/"/>
    <IPermission class="System.Security.Permissions.ZoneIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Zone="MyComputer"/>
    <IPermission class="System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Level="Minimal"/>
    <IPermission class="System.Net.DnsPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Unrestricted="true"/>
    <IPermission class="System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1"
    Unrestricted="true"/>
    </PermissionSet>

    The method that caused the failure was:
    Void <add_Clicked>b__0(System.Object, Microsoft.Office.InfoPath.ClickedEventArgs)

     

    Wednesday, March 21, 2007 3:47 PM

Answers

  • The reason why this environment failed is due to Code Access Security.  By default a DLL accessing the SharePoint OM will fail on the SharePoint side, unless you modify the given trust levels beyond their scope, create your own, or use the Full trust level.  Another way around the problem would be to install the assembly into the GAC (C:\Windows\assembly), which would require strongly naming the DLL.  None of these options should be taken lightly, so be sure to dive into CAS before deciding to go with any of these paths.  Also, you can look at CAS as it applies to InfoPath here: http://msdn2.microsoft.com/en-us/library/aa944687(VS.80).aspx.
    Tuesday, July 3, 2007 3:43 AM

All replies

  • I dont think the form is runing under the context of the site with the document library. You should change the code to load the SPWeb object using a different method (like using a SPSite object and a url).

    For example (not tested with infopath forms!):

    //load the site object from the current url
    using (SPSite site = new SPSite(this.Page.Url.ToString()))
    {

    //load the web object of the site that this page is a part of
    using (SPWeb web = site.OpenWeb())
    {

    SPUser user = web.CurrentUser;
    string s = user.Name;

    }

    }

    Thursday, March 22, 2007 1:51 AM
  • for get the current user,

    i always use the following code:

    SPControl.GetContextWeb(Context).CurrentUser;

    it's always right.

    Friday, March 23, 2007 3:29 AM
  • But in a InfoPath codebehind I have no "Context" object?
    Monday, March 26, 2007 9:02 AM
  • I'm working in InfoPath form 2007, too.
    From Form Code can't write some tasks to sharepoint 2007 tasks list.

    I used several methods, but they don't work,

    Can u help me to get access to sharepoint list from Form?

    May be U already had success!

    some error:
    "Microsoft.SharePoint.CoreResource" выдал исключение.
    в Microsoft.SharePoint.CoreResource.GetString(ResourceGroup rg, String name, Object[] values)
    в Microsoft.SharePoint.SPResource.GetString(String name, Object[] values)
    в Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
    в Microsoft.SharePoint.SPSite..ctor(String requestUrl)
    в protocol.FormCode.CTRL80_6_Clicked(Object sender, ClickedEventArgs e)
    в Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
    в Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)


    Thanks!!!!
    Best regards, Oleg.
    Wednesday, March 28, 2007 11:03 AM
  • Hi lucker,

    I'm having the same problem writing to SharePoint list from Infopath 2007 Form code. Did you get any answers on that? I would really appreciate if you could please share your solution.

     

    Thanks!

    Monday, July 2, 2007 12:58 PM
  • The reason why this environment failed is due to Code Access Security.  By default a DLL accessing the SharePoint OM will fail on the SharePoint side, unless you modify the given trust levels beyond their scope, create your own, or use the Full trust level.  Another way around the problem would be to install the assembly into the GAC (C:\Windows\assembly), which would require strongly naming the DLL.  None of these options should be taken lightly, so be sure to dive into CAS before deciding to go with any of these paths.  Also, you can look at CAS as it applies to InfoPath here: http://msdn2.microsoft.com/en-us/library/aa944687(VS.80).aspx.
    Tuesday, July 3, 2007 3:43 AM
  • In InfoPath form set Permission: In Tools -> Form Options -> Security & trust -> Full Trust & Publish
    Wednesday, February 10, 2010 12:54 PM