Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

SPWebPartCollection.Add(string dwp) fails, Not a safe control


Author: Robert Bogue MVP

View products that this article applies to.

Symptoms

When trying to add a web part via the SPWebPartCollection.Add(string dwp) method the following error is raised:

"An unhandled exception of type 'b' occurred in Microsoft.SharePoint.dll."
Additional Information: "A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe."

This error occurs whether the control is registered as safe or not.

↑ Back to the top


Cause

The SPWebPartCollection.Add() method is trying to use reflections to load the web part assembly referred to in the DWP string.� If that assembly's DLL file is not found in the Global Assembly Cache (GAC) or the other default load paths the error is raised.� In console or WinForms applications the web site's BIN directory where all web part assemblies are found are not a part of the�load path for the application.

↑ Back to the top


Resolution

Option 1: Install all web parts into the Global Assembly Cache (GAC)
This option has the benefit of being quick but may not be possible due to some web parts not being strongly named.� Additionally, it creates security concerns since all assemblies in the GAC have full code access security.

Option 2: Run your application from the /bin directory of the web site
This option allows the assemblies to be loaded even if not in the GAC because they are in the same directory as the application.� This is generally not advisable since the application must be in the /bin directory.

Option 3: Use the Web Service equivalent to accomplish the same objective
Web services always run within the context of the web server and therefore have a load path for the /bin directory where the assemblies are stored.� The WebPartPages web service's AddWebPart operation allows you to add a web part to the site without this limitation.

↑ Back to the top


Properties

COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

↑ Back to the top


Community solutions content disclaimer

Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

↑ Back to the top


Keywords: KB555292, kbhowto, kbpubtypecca, kbpubmvp

↑ Back to the top

Article Info
Article ID : 555292
Revision : 1
Created on : 4/21/2005
Published on : 4/21/2005
Exists online : False
Views : 396