Monday, November 7, 2011

Create MySite On Server

bool CreateMySiteOnServer(string serverUrl)
{
bool returnValue = false;
Logger.LogVerbose("CreateMySiteOnServer for URL:" + serverUrl);

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite lowPrivilegeSite = new SPSite(serverUrl);
SPSite site = new SPSite(serverUrl, lowPrivilegeSite.UserToken);
lowPrivilegeSite.Dispose();
using (site)
{
Logger.LogVerbose("Using Site:" + site.Url);

bool allowUnsafeUpdates = site.AllowUnsafeUpdates;
bool contextAllowUnsafeUpdates = SPContext.Current.Web.AllowUnsafeUpdates;
UserProfile profile;
try
{
site.AllowUnsafeUpdates = true;
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(serviceContext);
if (false == profileManager.UserExists(GetCurrentUser()))
{
Logger.LogVerbose("Creating User Profile.");
profile = profileManager.CreateUserProfile(GetCurrentUser());
}
else
{
Logger.LogVerbose("Getting User Profile.");
profile = profileManager.GetUserProfile(GetCurrentUser());
}
// Create Personal Site
Logger.LogVerbose("Creating Personal Site.");
profile.CreatePersonalSite();
// If success, we set return value - failure to CreatePersonalSite will throw an exception
returnValue = true;
}
catch (Exception ex)
{
Logger.LogException("CreateMySite.CreatePersonalSite (inner try)", ex);
}

site.AllowUnsafeUpdates = allowUnsafeUpdates;
SPContext.Current.Web.AllowUnsafeUpdates = contextAllowUnsafeUpdates;
}
}
);
}

catch (Exception ex)
{
Logger.LogException("CreateMySite.CreatePersonalSite (outer try) ", ex);
}

return returnValue;
}

Thursday, November 3, 2011

Get Taxonomy Term by ID

static bool GetTermById(Guid idToMatch, TaxonomySession spTaxonomySession, ref Term result)
{

foreach (TermStore ts in spTaxonomySession.TermStores)
{
if (0 == ts.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched TermStore: '" + ts.Name + "'");
return false;
}

foreach (Group group in ts.Groups)
{
if (0 == group.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched Group: '" + group.Name + "'");
return false;
}
foreach (TermSet set in group.TermSets)
{
if (0 == set.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched TermSet: '" + set.Name + "'");
return false;
}
foreach (Term term in set.Terms)
{
if (0 == idToMatch.CompareTo(term.Id))
{
result = term;
return true;
}
}
}
}
}
Console.WriteLine("FAILED - NOT FOUND");
return false;
}

Wednesday, March 2, 2011

Types of URL

http://msdn.microsoft.com/en-us/library/ms431831.aspx

Absolute URL that specifies a full path and that begins by specifying a protocol. For example, http://Server/[sites/]Web_Site/Lists/List_Title/AllItems.aspx.

Server-relative URL that is based on the server address and that begins with a forward slash, specifying a complete path from top-level Web site to file name. For example, /[sites/]Web_Site/Lists/List_Title/AllItems.aspx.

Web site-relative URL that is based on the address of a Web site object (SPWeb), that does not begin with a forward slash, and that specifies a complete path from the Web site's address to the file name. For example, Lists/List_Title/AllItems.aspx.

URL relative to a file or folder that is based on the folder containing the file that does not contain any forward slashes and that specifies the name of the file. For example, AllItems.aspx.

Thursday, February 3, 2011

A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe

http://geeklit.blogspot.com/2010/08/web-part-debug-error.html

Great explanation of the "A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe" error.

Friday, January 21, 2011

The annoying, "The site is not valid. The 'Pages' document library is missing" error when trying to set the DefaultPage property for a website.

The DefaultPage property points to a page in the PagesList, so the PagesList must be set to a valid document library before the DefaultPage property can be set.

Here's how to do this:


SPWeb newSite = mySite.Webs.Add(subsiteUrl, subsiteTitle, subsiteDescription, 1033, currentTemplate, true, false);
newSite.Update();
SPFile welcomePage = newSite.Folders[pagesFolder].Files[defaultPage];
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(newSite);
pubWeb.PagesListId = newSite.Folders[pagesFolder].DocumentLibrary.ID;
pubWeb.Update();
pubWeb.DefaultPage = welcomePage;
pubWeb.Update();

Friday, January 14, 2011

My Technical Writing

http://support.microsoft.com/kb/947379/en-us

Writing to Trace Log

Here’s some sample code for writing to the trace log.

using Microsoft.SharePoint.Administration;
private void WriteTraceLog(string logEntry)

{
SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local; SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, logEntry, cat.Name, cat.Area.Name);

}

The logfile’s here: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS(SP creates a new logfile every 30 minutes.)

References:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdiagnosticsservicebase.writetrace.aspx

http://technet.microsoft.com/en-us/library/ee748656.aspx

http://msdn.microsoft.com/hi-in/library/aa979590(en-us).aspx

Administrators can use the LogLocation property of the SPDiagnosticsService class to specify the location of the trace log to which to write.The actual registry key that specifies the location of the trace log is called LogDir and is located at the following location:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\WSS]

However, you should never edit this value directly.

(This usually defaults to: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS)