Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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).

share|improve this answer
    
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 '12 at 9:56
    
Deploy to the GAC, as suggested. –  James Love Feb 15 '12 at 9:58
    
I depolyed it GAC but without giving trust full the error still exist. –  Shayan Feb 15 '12 at 9:59
    
Did you do an iisreset? (i guess so, just checking). GAC deployment should fix it ... –  Frederik P. Feb 15 '12 at 10:07
    
thanks, it solved –  Shayan Feb 15 '12 at 10:34
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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