The application attempted to perform an operation not allowed by the security policy – DirectoryServices

I have developed a custom search and successfully deployed in our SharePoint 2007 (We have almost 16 sites and one document library, so we decided to filter the search according to the location, I will post the step by step development to the custom search later :)). My manager asked me to pick the location (ie. Organizational Unit (OU)) from Active Directory (AD) according to the user dynamically and I have written a method to pick the OU, you can see the post here. So far I didn’t get any problem but when I tried to deploy the web part to the SharePoint, I have got several errors and I managed to solve them but the following error gave me very hard time.

Security Exception: 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 ‘System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ failed.

Source Error:

Stack Trace:

I tried several solutions to fix this issue but no luck, because my C# code is trying to access the AD information which is in the different server, so there is no trusted permission from SharePoint, but I could access the details form the local machine. Finally I just read the error message carefully and modified the Trust Level in the SharePoint web.config to “Full” then it was working.

Please note: Changing this Trust Level might harm your SharePoint Security!

To do the changes:

Step1: Open the web.config file (Using Notepad OR Visual Studio)

Step2: Locate the <trust level=”WSS_Minimal” originUrl=”” /> tag in the web. config file. The default value is “WSS_Minimal”. I tried “WSS_Medium” but it didn’t solve the problem, sometimes it will solve yours.

Step3: Change the <trust level=”Full” originUrl=”” />

That’s all, it is working now. Happy coding n SharePointing! There are some other solutions for this related issue, you can try.

1. Changes on the .NET Framework 2.0 Configuration tool, Tim Huffam’s blog will show how to do the work around.

References:

1. http://support.microsoft.com/kb/555466

2. http://us.generation-nt.com/answer/using-system-directoryservices-sharepoint-webpart-help-35022312.html

3. http://geekswithblogs.net/timh/archive/2006/03/08/71714.aspx

Advertisements

, , , , , , , ,

  1. #1 by Kevin on December 31, 2012 - 11:15 pm

    Hi Rosh,sounds like you already have the code that will do the logic and now you just need to seal isnide a feature.You`re feature would be a web scoped feature with event receiver defined something like this:The you would create a class named HideNavEventReceiver or something that looks the same as in your feature XML definition file above. Your class inheriting from SPFeatureReceiver would have the following code (pretty much your code wrapped in a feature):public override void FeatureActivated(SPFeatureReceiverProperties properties) { using (SPWeb web = properties.Feature.Parent as SPWeb) { int i = 0;int sitesIndex = 0;int subsiteIndex = 0;web.AllowUnsafeUpdates = true;web.Update();PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);pw.Update();SPNavigationNodeCollection parentNodes = web.Navigation.QuickLaunch;for (i = 0; i < parentNodes.Count; i++){if (parentNodes[i].Title == "Sites"){sitesIndex = i;}if (parentNodes[i].Title == "Subsite1"){subsiteIndex = i;SPNavigationNodeCollection childrenNodes = parentNodes[sitesIndex].Children;SPNavigationNode childNode = parentNodes.Navigation.GetNodeById(parentNodes[subsiteIndex].Id);childNode.MoveToLast(childrenNodes);web.Update();}} } }Let me know if thats what you were looking for.

  2. #2 by Jody on June 16, 2014 - 10:11 pm

    What’s up, yuup this posst is really pleasant and I have learnned lott of things fdom it regarding
    blogging. thanks.

  1. Request for the permission of type ‘System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ failed – InfoPath 2007 forms Security Levels « Step
  2. Request for the permission of type ‘System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ failed – InfoPath 2007 forms Security Levels « Step

Please leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: