2009/08/18

SharePoint - using the rich text box in a custom user control

Frustration aplenty with this one. Logic dictated that the RichTextField control in SharePoint would have been the one to use...
but no, it's not (as i found out)...so how do you use that shiny nice Rich Text Box control in a custom web part or user control.


simple - so simple i'm sad to say it took me 1 1/2hrs discussing it with some like-minded fellows before we found out what it was (sad i know, but we got there in the end).
Add this to your ascx (if you're using a web user control):

1: <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

and here comes the simple bit..

1:


That's it...not that impressive but that's what you have to do..simple eh :)
Note: this is only available in MOSS 2007.

The article is published by http://msmvps.com/blogs/brianmadsen/archive/2008/03/14/sharepoint-using-the-rich-text-box-in-a-custom-user-control.aspx

2009/08/11

OutOfMemory Exception

I am using customList to add some requests from Anonymouse useres.

But when I used EventHandler it gives me exception "OutOfMemory exception" while my code was as the following:

SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPWeb web = SPControl.GetContextWeb(Context);
web.AllowUnsafeUpdates =true;

SPListItem item = web.Lists["MyList"].Items.Add();
item["RequestedName"] = textBoxName.Text;
item["DateRequestRecievd"] = DateTime.Now;
item["RequestedPhoneNo"] = textBoxMobileNo.Text;
item["RequestType"] = dropDownListRequestType.SelectedItem.Value;
item["RequestSubject"] = textBoxRequestSubject.Text;
item["state"] = textBoxState.Text;
item["IdentityNo"] = textBoxIdentityNo.Text;
item.Update();
web.AllowUnsafeUpdates =false;
});

I used that code because I know that "SPSecurity.RunWithElevatedPrivileges" imporsenates the current user permissions with "system Account" Permissions. But it doesn't work with Anonymouse users.


There the solution of this Issue Anonymouse doesn't have permission to access assembly. So, you need to Authonticate as an authonticated user. I created a user that will be used for imporsenation, "Domain\Visitors". I wrote the following code
1 SPSecurity.RunWithElevatedPrivileges(delegate()
2 {
3 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
4 {
5
6 using (SPWeb parentWeb = site.OpenWeb())
7 {
8 string userName = ConfigurationManager.AppSettings["contactUsUser"];
9 SPUser user = parentWeb.AllUsers[userName];
10 ////YOU CAN ALSO HARD CODE THE USER AS BELOW
11 //SPUser user = parentWeb.AllUsers["DOMAIN\\Vistors"];
12 SPUserToken token = user.UserToken;
13
14 using (SPSite s = new SPSite(SPContext.Current.Site.Url, token))
15 {
16 using (SPWeb web = s.RootWeb)
17 {
18 //MY LOGIC HERE
19 }
20 }
21
22 }
23
24 }
25 });

you can see my post on MSDN Forum