Month: January 2011

SharePoint 2010–NullReferenceException inside a RunWithElevatedPrivledges Block Using SPContext.Web

Dealing with anonymous access to a website, I found some rather interesting behavior where a Redirect handler I had written was throwing a NullReferenceException.  It turns out that the SPContext.Web property includes some fairly complex logic that under certain circumstances creates a new SPWeb instance. As a result, it may fail with the NullReferenceException, although that behavior is not documented (Big surprise for the MSCryptic fans…) In my case it dealt with an application page in the _layouts folder that was set for anonymous access using the UnsecuredLayoutsPageBase class.  My RunWithElevatedPrivileges code block was creating problems when trying to access certain properties using the SPContext.Current.Site object. The solution was to instantiate a local variable outside the using block to hold the user’s current site and then read properties from the local variable once inside the RunWithElevatedPrivileges block in order to avoid the NullReferenceException.  Here is the code: protected override void OnPreInit(EventArgs e) { // site and web objects working with the current user's privileges SPSite userSite = SPContext.Current.Site; SPSecurity.RunWithElevatedPrivileges(delegate() { // get new site and web objects working with elevated privileges using (SPSite thisSite = new SPSite(userSite.ID)) { //Code here using thisSite instead of SPContext.Current.Site.... } }...

Read More

HTML5 video on Windows Server 2008 R2

For some reason, Microsoft doesn’t like the idea of the average user running server as their desktop, but for those of us who develop SharePoint and don’t want to go through the hack of installing it on Windows 7 it can be rather limiting. I use virtual machines for some custom development, but also run Server 2008 R2 locally or remote into servers to do work/research.  One slightly annoying feature is the lack of user tools like the drive cleanup and all my lovely video codecs.  Enter the Desktop Experience feature… Unfortunately, codecs for H.264 and AAC are missing...

Read More

SharePoint 2010–ULS Logging

This feature is mainly for developers. Microsoft has added new classes which help to log and trace the errors and exceptions. Developer Dashboard: This records the time, resources used for a particular web page request and shows on the UI. Request usage database: This database contains all the information about requests and resource usage. SPMonitoredScope: This is the attribute and if you apply this to a class then you can save the complete details to the SharePoint usage database. With new API features it is very easy to implement or communicate with ULS logs by using SPDiagnosticsServiceBase class. If you have not already done so, I highly recommend you download the ULS Viewer tool.  It allows admins and developers to open a ULS log file and display its contents in a user friendly format. Users can then perform advanced functions such as filtering, sorting, highlighting, loading logs, appending logs, etc in order to single out the data that is important to the user. This information can be used to diagnose problems with machines running ULS services, or to monitor machines and the events they create in...

Read More

.NET Framework 4.0 Gacutil.exe (Global Assembly Cache Tool)

Latest gacutil is part of Microsoft Windows SDK for Windows 7 and .NET Framework 4. Remember you can’t use previews version of gacutil (1.1 or 2.0/3.5) to register dll built in framework 4.0. If you don’t want to install whole SDK you need to copy below files to server, only. Of course you need to install SDK on local machine to be able copy below files to server. 1. Files location: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools List of files required to gacutil work properly: gacutil.exe gacutil.exe.config 1033\gacutlrc.dll (can be placed in the same dir as gacutil.exe on the server) Another interesting thing about assembly in 4.0 framework is location of your assembly. Location changed from C:\Windows\assembly\ to C:\Windows\Microsoft.NET\assembly\GAC_MSIL Remember to unregister assembly you need to use commend: gacutil /u assemblyname (you can’t use assemplyname.dll) Additional information: Understanding The CLR Binder gacutil...

Read More

SharePoint 2010: Reset the Farm Passphrase

Imagine you’ve been handed a farm with no documentation,  or you have documentation and you don’t trust it, or you just forgot to document it.  You spend some time cleaning it up and making it nice and shiny with proper documentation.  Now you need to add a new server to the farm and you don’t have that all important new piece of information in 2010 called the Farm Passphrase.  Currently there is no way to retrieve the Farm Passphrase from SharePoint, however there is the option to reset the Passphrase to a new one by using PowerShell. In order to do this, you must be a member of the SharePoint_Shell_Access database role on the SharePoint Configuration database. Check Get-SPShellAdmin and Add-SPShellAdmin. Open the SharePoint 2010 Management Shell (Start > All Programs > Microsoft SharePoint 2010 Products > SharePoint 2010 Management Shell) and make sure you run as an administrator. Enter the following: $passphrase = ConvertTo-SecureString -asPlainText –Force The request for String: will appear Type the new passphrase and hit enter The PowerShell prompt will appear again, enter this: Set-SPPassPhrase -PassPhrase $passphrase -Confirm You will be asked to confirm the Passphrase  by re-entering it Click enter The system will ask Are you sure you want to perform this action? Type Y and click enter And now you’re able to use the Passphrase you just entered to join your new server...

Read More

SharePoint 2010–Search and Best Bets

SharePoint search is often one of the most underutilized features in SharePoint.  And while I couldn’t possibly hope to explain everything in SharePoint search in one post, I thought I would cover the concepts of ‘Best Bets’ and how to set them up.   Best bets essentially allow you to inject your chosen content at the top of search queries for certain keywords. Keyword terms are defined by an administrator to enhance search results. They can be used in queries from the Search box, the Search Center site, or Advanced Search. When a query includes a keyword term or one of its synonyms, the definition for that term and links to its featured locations or documents, called "Best Bets," are displayed prominently on the search results page, above the core search results in the Best Bets WebPart. As an example, when ever someone types ‘iedaddy’ I want to return two key items at the top: a link to my twitter feed link a link to my blog iedaddy.com By defining terms that are often used in searches as keywords, a site collection administrator can provide a standard glossary of names, processes, and concepts that are part of the "common knowledge" shared by members of an organization. When keywords have synonyms and associated Best Bet links, they also become handy tools for guiding users toward recommended resources. Site collection administrators typically...

Read More