Create a new Wiki Page

The code below adds a new wiki page and then inserts a web part into the new page’s rich text editor

SPList pagesList = web.Lists["Pages"];

SPFolder rootFolder = pagesList.RootFolder;

SPFileCollection fileCollection = rootFolder.Files;

string pageUrl = rootFolder.ServerRelativeUrl + "/MyNewPage.aspx";

//Create the page
SPFile newFile = fileCollection.Add(pageUrl, SPTemplateFileType.WikiPage);

//Checkout and add custom web part to the newly created page
newFile.CheckOut();

//Use a new Guid as the web part id
Guid storageKey = Guid.NewGuid();

string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));

//Get web part manager
using (SPLimitedWebPartManager manager = newFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))

{

//Initialize the web part and insert it into 'wpz' web part zone.
//Web part zone is hidden in Rich text editor and its name is fixed ('wpz')

MyWebPart webPart = new MyWebPart();

webPart.ID = wpId;

webPart.Title = "My New Page";

manager.AddWebPart(webPart, "wpz", 0);

string content = String.Format(CultureInfo.InvariantCulture
, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>"

, new object[] { storageKey.ToString("D") });

SPListItem item = newFile.Item;

item["WikiField"] = content;

item.Update();

}
newFile.CheckIn(string.Empty);

Hope that helps 🙂

A quick and easy way to read RSS in SharePoint Foundation 2010

Thanks to George Tsiokos for his Data view web part http://sharepointrss.com/, we just have to submit the form on the site. This will produce a .DWP file. Save the .DWP file to your local drive and the next step is to upload this .DWP file to a web part page in SharePoint and you can view the RSS feed. No installation required 🙂

Super user for SharePoint Web Application

I found the following warning message in EventViewer :

“Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.”

Resolved it by creating 2 user policies for the web application, 1 for super user and 1 for super reader. These users should not be farm administrators nor Application pool accounts.

Steps:
1. Go to Central Administration
2. Click the Application Management
3. Click the Manage web applications
4. Click the Web application and select User Policy from the Policy ribbon bar
5. Click Add Users
6. Select All Zones and click the Next button
7. Type domain\spSuperUser and give the user Full Control permission
8. Click Finish
9. Repeat Step 4 -8 for domain\spSuperReader

After adding the user policies, run the following PowerShell script:

$webApp = Get-SPWebApplication "http://url/&quot;
$webApp.Properties["portalsuperuseraccount"] = "domain\spsuperuser"
$webApp.Properties["portalsuperreaderaccount"] = "domain\spsuperreader"
$webApp.Update()

Please note if you use Claim based authentication the username format will be different.

Hope that helps

Add Content Editor Web Part into Site Template

In Onet.xml, find the module(s) that need to have a content editor web part and add the following:


<AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1">
          <![CDATA[
          <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
            <Title>My Web part</Title>
            <FrameType>None</FrameType>
            <Description>Say Hello Web Part</Description>
            <FrameState>Normal</FrameState>
            <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
            <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
            <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
            <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
                &lt;script&gt; 

                function SayHello() 
                { 
			alert('Hello');
                  	
                      //all done 
                      return; 
                    } 
                  } 
                }


                _spBodyOnLoadFunctionNames.push(&quot;SayHello&quot;) 

                &lt;/script&gt;
            </Content>
            <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
          </WebPart>
          ]]>

        </AllUsersWebPart>

Note some characters need to be encoded.

In case a web part is broken, use contents=1 query string, for example: http://myurladdress/default.aspx?contents=1. This will show a list of all the web parts on the page.

Cheers 🙂

Show subsite on top navigation

Showing subsite programmatically is not as straight forward as using the UI

Global navigation

I found out that this property is only available from PublishingWeb (instead of SPWeb object) which is contained in Microsoft.Sharepoint.Publishing.dll. By default VS 2010 does not include the dll in the reference. After adding the dll, use the code below to show sub sites

PublishingWeb publisher = PublishingWeb.GetPublishingWeb(myWeb);
                        
publisher.Navigation.GlobalIncludeSubSites = true;
publisher.Update();
publisher.Close();

            

Hope that helps

Exception from HRESULT: 0x8107026E0

I got that exeception when I tried to deploy my solution. The problem was because I renamed my web template but I didn’t change the name attribute of the web template itself.

Hope that can help 🙂

Sharepoint list type id

Something that I always forget. You can also find it in http://msdn.microsoft.com/en-us/library/ms439434.aspx

100 Generic list

101 Document library

102 Survey

103 Links list

104 Announcements list

105 Contacts list

106 Events list

107 Tasks list

108 Discussion board

109 Picture library

110 Data sources

111 Site template gallery

112 User Information list

113 Web Part gallery

114 List template gallery

115 XML Form library

116 Master pages gallery

117 No-Code Workflows

118 Custom Workflow Process

119 Wiki Page library

120 Custom grid for a list

130 Data Connection library

140 Workflow History

150 Gantt Tasks list

200 Meeting Series list

201 Meeting Agenda list

202 Meeting Attendees list

204 Meeting Decisions list

207 Meeting Objectives list

210 Meeting text box

211 Meeting Things To Bring list

212 Meeting Workspace Pages list

301 Blog Posts list

302 Blog Comments list

303 Blog Categories list

1100 Issue tracking

1200 Administrator tasks list