prashant mulay

Hi,

I want to create a asp.net2.0 application, from where user can create his own site and templates for wss3.0 using c# language.

Please anybody let me know how to do this.

I gave the below code for displaying all the sites in wss3.0 in my system, but it is prompting a error called "An object reference not found".

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(GetSitesAndGroups);

SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups);

}

protected void GetSitesAndGroups()

{

string userList = SPEncode.HtmlEncode(TextBox1.Text) +

" is a user in the following webs:<BR>";

SPSite mySite = SPContext.Current.Site;

SPWebCollection allWebs = mySite.AllWebs;

foreach (SPWeb subWeb in allWebs)

{

string listGroups = "";

/*Use AllUsers not Users to ensure you find the user*/

SPUserCollection allUsers = subWeb.AllUsers;

foreach (SPUser user in allUsers)

{

if (user.LoginName.ToUpper() == TextBox1.Text.ToUpper())

{

SPGroupCollection allGroups = user.Groups;

foreach (SPGroup group in allGroups)

{

listGroups += SPEncode.HtmlEncode(group.Name) + " ";

}

userList += subWeb.ServerRelativeUrl.ToString() +

" -- " + listGroups + "<BR>";

}

}

}

Label1.Text = userList;

}

Thank you,

Prashant



Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

Curtis Ruppe (MicroStaff IT)

Two things: First off, are you running that ASP.NET application within the context of the SharePoint environment. Typically this is done by copying that web application into the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS directory. If not, then that is your problem, or if you are running that on a different box than SharePoint, then you would run into the same issue. The second thing (not related to problem) is your elevated privileges code. You should not use SPContext.Current.Site, but instead use SPSite mySite = new SPSite(SPContext.Current.Site.ID);



Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

Ishai Sagi

I agree with Curtis - you are using "SPSite mySite = SPSite mySite = SPContext.Current.Site; .Site;", but you say you are running from an ASP.NET2 application and not from sharepoint. that means there is no "SPContext.Current" object - which is why you are getting an error.

Alternatively you can load the SPSite using a url (if the asp.net application is running on the same server as sharepoint):

Code Snippet
SPSite mySite = new SPSite ("http://mysharepointsite");

You should also make sure you dispose of the SPSite and SPWeb objects.






Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

prashant mulay

Thank you Ishai.

It Worked. How to create a subsite from asp.net2.0 application I done the below code, but it is giving me below error.

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

And my code is

SPWebCollection sp = mySite.AllWebs;

SPWeb mynewsite = sp.Add("Site_Name");

//sp.Add("FullDoc","Doc","Doc",1760,SPWebTemplate.WebTemplateWIKI,true,false);

Please guide me.

Regards,

Prashant





Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

Curtis Ruppe (MicroStaff IT)

Within the ASP.NET page, you need to add a FormDigest control.



Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

Ishai Sagi

you should run a search on that error - there are plenty of articles about it.

here is one (for sharepoint 2003, but the reasons are probably the same) http://epham.wordpress.com/2007/01/22/how-to-fix-security-validation-errors-in-sharepoint-aspnet-page/






Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

prashant mulay

Hi Crutis/Ishai,

I have done the same, but getting an error called object reference not set. Please help me.

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<cc1:FormDigest ID="FormDigest1" runat="server">

</cc1:FormDigest>

<div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>

<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>

</div>

</form>

</body>

</html>

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
  Microsoft.SharePoint.WebControls.SPControl.EnsurePageUri() +81
  Microsoft.SharePoint.WebControls.SPControl.get_PageUri() +24
  Microsoft.SharePoint.WebControls.FormDigest.RenderCore(SPWeb web) +100
  Microsoft.SharePoint.WebControls.FormDigest.OnPreRender(EventArgs e) +143
  System.Web.UI.Control.PreRenderRecursiveInternal() +148
  System.Web.UI.Control.PreRenderRecursiveInternal() +233
  System.Web.UI.Control.PreRenderRecursiveInternal() +233
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4435





Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

Curtis Ruppe (MicroStaff IT)

I seem to recall issues with having the ID attribute with FormDigest. Make sure you control only does <cc1:FormDigest runat="server" />. Also, verify cc1 refers to the Microsoft.SharePoint.WebControls assembly. Also, to use the FormDigest and the SharePoint OM, this web page must reside within the Context of SharePoint Server. Typically this is done for web pages by using the _layouts directory as a starting point for your webapplication (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\YourWebApp). You would access your custom web creation web page, by going to the web-specific url. For example: http://<server>/subweb1/_layouts/YourWebApp/CustomCreate.aspx would create this new subweb under subweb1, whereas http://<server/_layouts/YourWebApp/CustomCreate.aspx would create the new subweb under the root. I hope this helps.



Re: SharePoint - Development and Programming Create a wss site from asp.net 2.0

OminiuS

hi!

What should i do if my web site is not in the _layouts directory
I mean, if my web site is an independant web site (from SharePoint) in IIS.

thx