public void CreateWebPartPage(SPWeb spWeb, string libraryName, string folderName, string fileName, string text, Hashtable properties, bool overWrite)
{
try
{
text = HttpUtility.HtmlDecode(text);
spWeb.AllowUnsafeUpdates = true;
string pageTemplate = "spstd1.aspx"; //WebPage
#region Add a WebPartPage
// Prepare the source file, assuming the Web Part Page templates live in <Installation Path>/Template/<LCID>/<web template>/doctemp/smartpgs
string sourceFilePath = SPUtility.GetGenericSetupPath("Template//") + spWeb.Language.ToString() + "//" + spWeb.WebTemplate + "//doctemp//smartpgs//" + pageTemplate; ;
System.IO.StreamReader sr = new System.IO.StreamReader(sourceFilePath);
string content = sr.ReadToEnd();
// Assuming the Web Part Page template has a Title Bar Web Part with the title place holder "_TitlePlaceHolder_"
//string oldContent = "<WebPartPages:WebPartZone runat=/"server/" Title=/"loc:TitleBar/" ID=/"TitleBar/" AllowLayoutChange=/"false/" AllowPersonalization=/"false/"/>";
//string newContent = "<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" width=/"100%/">" +
// "<tr>" +
// "<td style=/"width:100%;/"><table border=/"0/" cellpadding=/"0/" cellspacing=/"0/">" +
// "<tr>" +
// "<td vAlign=/"middle/" align=/"left/" style=/"padding-left:5;/">" +
// "<img src=/"/_layouts/images/wpicon.gif/" align=/"absmiddle/" alt=/"Web Part Page Title Bar image/" title=/"Icon/" />" +
// "</td>" +
// "<td title=/"/" style=/"padding-left:5px;/"><img height=/"3px/" src=/"/_layouts/images/blank.gif/" alt=/"/" />" +
// "<br/><span class=/"ms-pagetitle/">" + fileName + "</span><br>" +
// "<img height=/"3px/" src=/"/_layouts/images/blank.gif/" alt=/"/" /><br/>" +
// "</td>" +
// "</tr>" +
// "</table></td>" +
// "</tr>" +
// "</table>";
//content = content.Replace(oldContent, newContent);
// Save the target file into the database
fileName += ".aspx";
SPFolder shareFolder = spWeb.GetFolder(spWeb.Url + "/" + libraryName); //("Shared Documents");
if (folderName.EndsWith("/"))
{
folderName = folderName.Substring(0,folderName.LastIndexOf("/"));
}
if (folderName.Equals(spWeb.Url + "/" + libraryName) || folderName.Equals(spWeb.Url + "/./" + libraryName))
{
folderName = folderName + "/";
}
if (folderName.Contains(spWeb.Url + "/./" + shareFolder.Url + "/"))
{
folderName = folderName.Substring((spWeb.Url + "/./" + shareFolder.Url + "/").Length);
}
else if (folderName.Contains(spWeb.Url + "/" + shareFolder.Url + "/"))
{
folderName = folderName.Substring((spWeb.Url + "/" + shareFolder.Url + "/").Length);
}
string[] folderList = folderName.Split('/');
string tag = shareFolder.Url;
for (int i = 0; i < folderList.Length; i++)
{
string folderSubName = folderList[i];
SPFolder subFolder = spWeb.GetFolder(tag + "/" + folderSubName);
if (!(subFolder != null && subFolder.Exists))
{
shareFolder.SubFolders.Add(tag + "/" + folderSubName);
}
tag = tag + "/" + folderSubName;
}
SPFolder folder = spWeb.GetFolder(tag);
string folderPath = folder.Url;
string targetFilePath = spWeb.Url + "/" + folderPath + "/" + fileName;
// Convert the string into UTF8 encoded bytes
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
System.Byte[] contentBytes = encoding.GetBytes(content);
System.Byte[] bytes = new System.Byte[contentBytes.Length + 3];
// Adding UTF8 byte order mark
bytes[0] = 0xEF;
bytes[1] = 0xBB;
bytes[2] = 0xBF;
contentBytes.CopyTo(bytes, 3);
SPFileCollection fileCollection = spWeb.Files;
if (overWrite)
{
SPFile spFile = spWeb.GetFile(targetFilePath);
if (spFile != null && spFile.Exists)
{
SPFolder spFolder = spWeb.GetFolder(folderPath);
spFolder.Files.Delete(targetFilePath);
}
}
else
{
SPFile spFile = spWeb.GetFile(targetFilePath);
if (spFile != null && spFile.Exists)
{
return;
}
}
fileCollection.Add(targetFilePath, bytes);
#region Add a WebPart to this new WebPartPage
string mZone = "Top";
SPWebPartCollection spWebColl = spWeb.GetWebPartCollection(targetFilePath, Microsoft.SharePoint.WebPartPages.Storage.Shared);
//SPLimitedWebPartManager spWebMan = spWeb.GetLimitedWebPartManager(targetFilePath, Microsoft.SharePoint.WebPartPages.Storage.Shared);
StringBuilder sbstr = new StringBuilder();
sbstr.Append("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
sbstr.Append("<WebPart xmlns=/"http://schemas.microsoft.com/WebPart/v2/" >");
string title = "Content Editor Web Part (WebPublisher)";
sbstr.Append("<Title>" + title + "</Title>");
sbstr.Append("<Description>Use for formatted text, tables, and images</Description>");
sbstr.Append("<ZoneID>" + mZone + "</ZoneID>");
sbstr.Append("<Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>");
sbstr.Append("<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>");
sbstr.Append("<!-- Specify initial values for any additional base class or custom properties here. -->");
sbstr.Append("<Content xmlns=/"http://schemas.microsoft.com/WebPart/v2/ContentEditor/"> ");
sbstr.Append("<![CDATA[ ");
#region Add HTML to the WebPart
sbstr.Append("<table border=/"0/" style=/"table-layout: fixed;/">");
sbstr.Append("<tr>");
sbstr.Append("<td valign=/"top/">");
sbstr.Append(text);
sbstr.Append("</td>");
sbstr.Append("</tr></table>");
sbstr.Append(" ]]> ");
#endregion
sbstr.Append(" </Content> ");
sbstr.Append("</WebPart>");
spWebColl.Add(sbstr.ToString());
#endregion
#region add properties to the WebPart
SPFile webPage = spWeb.GetFile(targetFilePath);
SPList libList = spWeb.Lists[libraryName];
foreach (object obj in properties.Keys)
{
string key = obj.ToString();
if (!libList.Fields.ContainsField(key))
{
libList.Fields.Add(key, SPFieldType.Note, false);
//libList.Fields.Add(key,SPFieldType.Note,false,true,new System.Collections.Specialized.
}
}
libList.Update();
spWeb.Update();
if (webPage != null && webPage.Exists)
{
foreach(object obj in properties.Keys)
{
string key = obj.ToString();
string value = properties[key].ToString().TrimStart().TrimEnd();
webPage.Properties.Add("AVE" + key, value);
webPage.Item[key] = value;
}
}
webPage.Properties.Add("TextContent", text);
webPage.Item.Update();
webPage.Update();
spWeb.Update();
#endregion
#endregion
spWeb.AllowUnsafeUpdates = false;
}
catch (Exception e)
{
throw e;
}
}