0

I write a webpart in C# for sharepoint 2007 .When i try to create instance of SPSite or SPWeb , for example:

  SPSite sit = new SPSite("http://sharepoint");

or

 SPSite sit = SPContext.Current.Site;

Or even when I :

                  SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    int t = 0;
                });

I get the below security execption :

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.

How can I solve it?

1 Answer 1

5

So...

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Change the application's trust level...

In order to deploy Web Parts that are able to access resources that require a higher trust level, you have three options: Raise the trust level for the entire webapplication, Deploy your Web Parts to the Global Assembly Cache (GAC), or Create your own custom policy.

A quick and dirty way to check if this is the problem: Open the web.config file and search for a line saying:

<trust level="..." />

set level to Full

Now execute your code again... This should work. Although this is not a good way.

You should deploy your dll's in the GAC or the APP_BIN folder (I prefer the GAC).

5
  • thanks, it worked , but at now I'm on my developing computer and didn't deployed to main sharepoint server , How can I solve it without giving trust level to full?
    – Shayan
    Feb 15, 2012 at 9:56
  • Deploy to the GAC, as suggested.
    – James Love
    Feb 15, 2012 at 9:58
  • I depolyed it GAC but without giving trust full the error still exist.
    – Shayan
    Feb 15, 2012 at 9:59
  • Did you do an iisreset? (i guess so, just checking). GAC deployment should fix it ... Feb 15, 2012 at 10:07
  • How? If I may ask Feb 15, 2012 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.