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);
Tuesday, February 12, 2008
Relative URL in Sharepoint is incorrectly resolved
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
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