Tuesday, February 19, 2008

Find localized name of the Everyone group

Today a colleague asked me if there is any way to find out what the Everyone group is called in a localized version of Windows. In a Swedish Windows, this group is kalled "Alla" and in a German version it's called "Jeder" etc.

There is an easy way to do this with .NET with the (new) .NET 2.0 Security functions.

SecurityIdentifier si = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
NTAccount everyOneGroup = si.Translate(typeof(NTAccount)) as NTAccount;
if (everyOneGroup != null) {
Console.WriteLine(everyOneGroup.Value); // now contains the localized name
}

Tuesday, February 12, 2008

Relative URL in Sharepoint is incorrectly resolved

Today I had a problem with the TreeView WebControl. I wanted to set the NavigateUrl to a relative url in Sharepoint in one of my webparts (I allow user configuration of this property so this must be resolved correctly).

When I set the NavigateUrl to "test.aspx" it's resolved to: "_catalogs/masterpage/test.aspx" which probably has something to do with the fact that the webpart manager containing my webparts is declared in the masterpage.

The solution was simple: Use Page.ResolveUrl before setting the url. This correctly resolves the relative url before it's set to the TreeView:

string relativeUrl = "test.aspx";
treeNode.NavigateUrl = Page.ResolveUrl(relativeUrl);

Friday, February 1, 2008

CSS reference map!

There are a lot of css classes defined in Sharepoint and it's not always easy to know what they look like. There is however a great site that lists all sharepoint css classes with examples:

http://www.heathersolomon.com/content/sp07cssreference.htm

Friday, January 25, 2008

Custom TreeView with a Select (onclick) event

One thing that disturbs me a bit is that the TreeView control does not have an event that is triggered when the user clicks an already selected item (in my case, I need to know about this so that I can refresh data. The SelectedNodeChanged is never fired when you click on an already selected item).

So wondered if it is possible to create my custom implementation of the TreeView and it turned out to be quite easy to do after a quick check in the reflector

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GB.ControlLib {

public class GTreeView : System.Web.UI.WebControls.TreeView
{

private static readonly object TreeViewSelected = new object();

public event EventHandler Select
{
add { base.Events.AddHandler(TreeViewSelected, value); }
remove { base.Events.RemoveHandler(TreeViewSelected, value); }
}

protected virtual void OnSelect(EventArgs e)
{
EventHandler handler = (EventHandler)base.Events[TreeViewSelected];
if (handler != null) {
handler(this, e);
}
}

internal void RaiseOnSelect()
{
this.OnSelect(EventArgs.Empty);
}

protected override void RaisePostBackEvent(string eventArgument)
{
base.RaisePostBackEvent(eventArgument);


if (eventArgument.Length != 0) {
char ch = eventArgument[0];
string val = HttpUtility.HtmlDecode(eventArgument.Substring(1));
// Pathseparator in eventArgument is backslash, so we must temporary set the separator
char oldPathSeparator = PathSeparator;
PathSeparator = '\\';
TreeNode node = FindNode(val);
PathSeparator = oldPathSeparator;

if (ch == 's') {
if (node != null) {
if ((node.SelectAction == TreeNodeSelectAction.Select) || (node.SelectAction == TreeNodeSelectAction.SelectExpand)) {
RaiseOnSelect();
}
}
}
}
}


}


}












Friday, January 18, 2008

RichText control in Sharepoint

Today I needed a RichText editor in a sharepoint webpart, so after some googling and using the reflector I found a class in the Microsoft.SharePoint.WebControls namespace that fits my needs, InputFormTextBox!

The control is not well documented (the same as all other controls in the Microsoft.SharePoint.WebControls namespace) but it's quite easy to use:

InputFormTextBox richTextEditor = new InputFormTextBox();
richTextEditor.RichText = true;
richTextEditor.RichTextMode = SPRichTextMode.FullHtml;
// (Can be set as FullHtml, Compatible or HtmlAsXml

The text entered into the control can be get/set in the Text property.

Set the richTextEditor.MultiLine to true if you want to enable multiline editing.

You can control the height and width in the same way as you use the regular TextBox control from the System.Web.UI.WebControls namespace.

References:
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.inputformtextbox.aspx
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sprichtextmode.aspx

Wednesday, January 16, 2008

How to determine the current database in MSSQL

Today I wanted to know what database I currently was connected to in an SQL-script (since I dynamically set the database name when calling SQLCMD) and found out that there is a simple function for this: db_name()

SELECT DB_NAME()

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

Thursday, December 27, 2007

Impersonation in ASP.NET causes [COMException (0x80072020): An operations error occurred. ]

When you run code that uses DirectorySearcher, DirectoryEntry or other classes that communicates with network resources from a webpart in a Sharepoint site, you recieve a: [COMException (0x80072020): An operations error occurred. ]

This is caused by the fact that when a user is authenticated against a sharepoint server using NTLM or Kerberos, a "secondary token" is sent to the server that it uses to authenticate the user. This token cannot be used to authenticate the current user against another server (e.g. a domain controller).

This can be circumvented by reverting the impersonation to the application pool account used by IIS (if this account has access to Active Directory) with the following code (this is equal to running with impersonation set to false in web.config):


using System.Web.Hosting;
...
...

// Code here runs as the logged on user

using (HostingEnvironment.Impersonate()) {
// This code runs as the application pool user
     DirectorySearcher searcher ...
}

// Code here runs as logged on user again