views:

52

answers:

2

I have created a WebPart. When I add the WebPart to a page I get the error below. Error MEssage

If I delete what is in my constructor for my WebPart it will then add to the page. Then if I deploy my code with the WebPart all ready on a page the WebPart will display correctly. I can't add it to other pages I get the error before.

So I presume that there is a problem with the constructor. The line that is breaking it is

var site = new SPSite("http://website/site");

This has been found by commenting code out. Can you not create an SPSite when installing a WebPart?

A: 

yes we can use spsite object.

Can you change your code like

 SPSite site = new SPSite("http://website/site");

let me know if it works.

Hojo
A: 

Do you have your constructor code wrapped in a try/catch block? It may be throwing an exception for some reason.

Otherwise, try moving your constructor code to OnInit to see if that makes a difference.

Paul Lucas
Linda, I tried it out and got the same results as you. It seems that instantiating an SPSite like that gives a SecurityException ("Request Failed"). I also tried the SPSite instantiation in OnInit and that worked!
Paul Lucas