Wednesday, February 20, 2008

Sharepoint naming

The English language is very old, it has many words and some crazy spelling.

In the days of yore, there were hardly any words need to describe some factor of life. English has only "love", in Greek there are several different words for love varying from maternal, paternal and amorous.

Along came the interweb and words like portal, site , web, web site and others were panel beaten into whatever holders were needed. Suddenly there was a lot of similar things and only a few words.

In sharepoint it is even worse. ever tried to Google for "sharepoint web portal feature", it just doesn't work. The naming of objects in Sharepoint is cunfusing and a real put off for everyone.

I suggest that future versions use words from other languages.

For example, use the word "poortaal" (from Dutch) to mean the SPPortal. No confusion, everyone (except the Dutch) will be mistaken that you mean the main portal that hosts the sites.

Thursday, February 14, 2008

Serialize Export Webparts

I needed to add webparts to a provisioned site. When the feature for the site creation began, webparts (all custom) had to be added and configured.

Easy enough, but how do you get the custom web parts out?

I found this on a http://sharepointex.blogspot.com/2007/09/managing-web-parts-programmatically.html

Was not exactly what I needed. So I used the gettype(), not that thier are two kinds of webparts, one from sharepoint and the other from asp.net


Note that this does not run, it will throw exceptions when trying to cast the web parts in the foreach, but I ran it in debug and dragged the execution point and changed the type string to get the files.

It was never meant to run more than once.


private void SerialzeWebparts(SPWeb curWeb)
{
SPLimitedWebPartManager sPLimitedWebPartManager = curWeb.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);

//might not be this kind of webpart
foreach (Microsoft.SharePoint.WebPartPages.WebPart wp in sPLimitedWebPartManager.WebParts)
{
if (((object)wp).GetType().ToString() == "Microsoft.SharePoint.Portal.WebControls.BlogView")
{
WebPartExportMode tempMode = wp.ExportMode;
wp.ExportMode = WebPartExportMode.All;

string fileName = String.Format(@"C:\Temp\{0}_{1}.txt", wp.ID, wp.Title);
System.Xml.XmlTextWriter wrt = new System.Xml.XmlTextWriter(fileName, Encoding.UTF8);

sPLimitedWebPartManager.ExportWebPart(wp, wrt);
wrt.Close();

wp.ExportMode = tempMode;
}
}

//might not be this kind of webpart
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in sPLimitedWebPartManager.WebParts)
{
if (((object)wp).GetType().ToString() == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")
{
WebPartExportMode tempMode = wp.ExportMode;
wp.ExportMode = WebPartExportMode.All;

string fileName = String.Format(@"C:\Temp\{0}_{1}.txt", wp.ID, wp.Title);
System.Xml.XmlTextWriter wrt = new System.Xml.XmlTextWriter(fileName, Encoding.UTF8);

sPLimitedWebPartManager.ExportWebPart(wp, wrt);
wrt.Close();

wp.ExportMode = tempMode;
}
}

}

Thursday, February 7, 2008

Add List items to a Custom List

Well, as you know, much of sharepoint's help looks like it was code generated. That's why we blog.

Anyway enough whining. I needed to add list items to a custom list (this one is from a feature activating event):


const SYSTEMSITEMENULISTTEMPLATENAME = "mycustomlist";
const SYSTEMSITEMENULINKSLISTDESCRIPTION = "description";

public void CreateSystemSiteMenuLinks(SPFeatureReceiverProperties properties, SPWeb curWeb)
{

//get instance of the list template from the web
SPListTemplate menuListTemplate = curWeb.ListTemplates[SYSTEMSITEMENULISTTEMPLATENAME];
//make a list with the template
curWeb.Lists.Add(SYSTEMSITEMENULINKSLISTNAME, SYSTEMSITEMENULINKSLISTDESCRIPTION, menuListTemplate);
//look up the custom content type
SPContentType systemMenuItemContentType = curWeb.Lists[SYSTEMSITEMENULINKSLISTNAME].ContentTypes["System Menu Item"];

//add item to the list, its actually for a menu site navigation on the My Site
AddMenuItems(curWeb.Lists[SYSTEMSITEMENULINKSLISTNAME].Items.Add(), "My Home", curWeb.Site.Url, systemMenuItemContentType.Id);
AddMenuItems(curWeb.Lists[SYSTEMSITEMENULINKSLISTNAME].Items.Add(), "My Profile", curWeb.PortalUrl + "", systemMenuItemContentType.Id);
AddMenuItems(curWeb.Lists[SYSTEMSITEMENULINKSLISTNAME].Items.Add(), "My Document", curWeb.PortalUrl + @"Person.aspx?accountname=" + System.Security.Principal.WindowsIdentity.GetCurrent().Name, systemMenuItemContentType.Id);
}


private void AddMenuItems(SPListItem sPListItem, string title, string menuUrl, SPContentTypeId systemMenuItemContentTypeId)
{

sPListItem["ContentTypeId"] = systemMenuItemContentTypeId;
sPListItem["Title"] = title;
sPListItem["Menu URL"] = menuUrl;

sPListItem.Update();
}

Wednesday, February 6, 2008

eboostr virtual machine performance

If you are like me and spending your waiting for virtual machines to run the Sharepoint resets and so on, drives you nuts, try eboostr .

eboostr is the windows XP version of Ready Boost. Ready Boost is only available in Vista.

Anyways download it at http://www.eboostr.com , its definitely worth it.




Friday, February 1, 2008

Swapping files in Sharpoint and My Site

I needed to swap the default page for new one with different webparts.

Obviously change the site definition template was out of the question.

And naturally finding out why this:

SPFile standardDefaultPage = curWeb.GetFile(SHAREPOINTDEFAULTPAGE);
SPFile newDefaultPage = curWeb.GetFile(MYSITENEWDEFAULTPAGE);

standardDefaultPage = newDefaultPage

does not work, just made no sense. Then I realized that sharepoint sometimes knows the default page even if its renamed.

So then I tried this.

The procedure is called from a custom feature activated in the My Site creation.





const string SHAREPOINTDEFAULTPAGE = "default.aspx";
const string MYSITENEWDEFAULTPAGE = "defaultNew.aspx";

public void SwapDefaultPages(SPFeatureReceiverProperties properties, SPWeb curWeb)
{
try
{

SPFile newDefaultPage = curWeb.GetFile(MYSITENEWDEFAULTPAGE);

newDefaultPage.CopyTo(SHAREPOINTDEFAULTPAGE, true);
}
catch (Exception ex)
{
UpdateLog(ex.Message + ex.StackTrace + ex.Source, EventLogEntryType.Error);
}

UpdateLog("done SwapDefaultPages");
}


and it worked.