Create a Wiki page programmatically <script runat="Server">

It doesn't seem like this should be so difficult but I would just like to create a Wiki page in the UserCreated event. Is there any way to call the CreateWebPage.aspx and pass it params? Seems like this is just way to much work for something so trivial - outside of SharePoint. The word overengineering comes to mind.

Anyone know how to do this?

[362 byte] By [Thomas.Goddard] at [2008-1-2]
# 1

How about using:

Microsoft.SharePoint.SPFileCollection.Add Method (String, SPTemplateFileType)

SPTemplateFileType is an enum and allows you to specify a regular web page or a wiki...

/HH

HH at 2007-9-13 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 2
Ok but when I call this method from my registration page, the page automatically redirects to the login page. Does this method require special permissions from the caller?
Thomas.Goddard at 2007-9-13 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 3
Here is the code for what I am trying to do. It bounces me out to the login page without an exception or error on the page. I am guessing that it requires special permission for this user to add the wiki page?
Code Snippet <script runat="Server">
public void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateUserWizard wizard = sender as CreateUserWizard;
if (wizard != null)
{
Roles.AddUserToRole(wizard.UserName, "RegisteredUser");
string username = wizard.UserName;
string text = username += ".aspx";
SPWeb spWeb = SPContext.Current.Web;
Guid guid = new Guid("187b57ed-d8c2-45e6-b020-18b45bc8baf1");
SPList list = spWeb.Lists[guid];

string strUrl = "/Wiki/" + text;
string error = "";
if (spWeb.GetFile(strUrl).Exists)
error = "User wiki exists";

SPListItem item = list.RootFolder.Files.Add(strUrl, SPTemplateFileType.WikiPage).Item;
item["Title"] = username;
item["Tags"] = "Users";
item.UpdateOverwriteVersion();
}
}

</script>

Thomas.Goddard at 2007-9-13 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 4
Ok for anyone out there ripping their eyeballs out because there is 0 SharePoint documentation and the implementation is whack, here is the code. It will work with many other list types as well. Enjoy it and if you have any issues, have fun googling until your brain falls out because MS sure hasn't posted ***.
Code Snippet

public void CreateUserWiki(string username)
{
string text = username + ".aspx";
WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();
SPWeb spWeb = SPContext.Current.Web;
Guid guid = new Guid("187b57ed-d8c2-45e6-b020-18b45bc8baf1");
SPList list = spWeb.Lists[guid];

string strUrl = "/Wiki/" + text;
if (spWeb.GetFile(strUrl).Exists)
list.RootFolder.Files.Delete(strUrl);

spWeb.AllowUnsafeUpdates = true;
SPListItem item = list.RootFolder.Files.Add(strUrl, SPTemplateFileType.WikiPage).Item;
item["Tags"] = "Users";
item.UpdateOverwriteVersion();
spWeb.AllowUnsafeUpdates = false;

wic.Undo();
}


It goes something like this "Clunk clank, clunk clank, crankkkkk, screeeeeech, thud!!!" It works!
Thomas.Goddard at 2007-9-13 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 5
Hi, I was wanting to use this in some inline script on a sharepoint page... Do you need to use this line "WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();" in order to create the wiki site?
ChadClarke at 2007-9-13 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...

SharePoint Products and Technologies

Site Classified