|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHere is a How-to for integrating the FCKEditor with SharePoint as a Web Part. Also, this article helps you implement a custom image browser for this Web Part. BackgroundI was searching the Internet a lot in order to find how to integrate a WYSIWYG editor such as TinyMCE or FCKEditor with the SharePoint platform, but without luck. So, I did it myself. Why bother? Both SharePoint Server (MOSS 2007) and the free version WSS 3.0 has integrated WYSIWYG editors. But they lack some features:
So, I tried to implement one of the most used WYSIWYG editors in the world, FCKEditor. You can download it here. For this project, I used the The idea is simple; to use FCKEditor instead of the Content Editor Web Part. Using the codefckeditor.zip is complete FCKeditor with the fileUpload ASPX page and code. FCKMossEditor is a Web Part VS2005 project. Step 1: Creating a Web PartSo we need a Web Part. I will not describe here what a Web Part is, because I assume that everyone who uses SharePoint knows what a Web Part is. In display mode, our Web Part will render HTML to SharePoint users, and in editor mode, we will use FCKEditor to manage the content. I created a Web Part in Visual Studio 2005. The easiest way to do it is to download Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions and to install it. Then, just use New>Project>C#> SharepPoint> Web Part project template.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace FckMossEditor
{
[Guid("9edcd0c0-c649-48d1-8acd-bd6c9b01a030")]
public class FckMossEditor : System.Web.UI.WebControls.WebParts.WebPart
{
public FckMossEditor()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;
namespace FckMossEditor
{
[Guid("7de64613-aa8e-46bb-a7cd-ed36ea03f159")]
public class FckMossEditor :
System.Web.UI.WebControls.WebParts.WebPart, IWebEditable
{
public FckMossEditor()
{
this.ExportMode = WebPartExportMode.All;
}
private string displayText = "Hello World!";
[WebBrowsable(true), Personalizable(true)]
public string DisplayText
{
get { return displayText; }
set { displayText = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(displayText);
}
#region IWebEditable Members
EditorPartCollection IWebEditable.CreateEditorParts()
{
List
Here, we have our Step 2: Configure FCKEditor
FCKConfig.ToolbarSets["MyToolbar"] = [
['Cut','Copy','Paste','PasteText','PasteWord'],
['Undo','Redo','-','Bold','Italic','Underline','StrikeThrough'],
'/',
['OrderedList','UnorderedList','-','Outdent','Indent'],
['Link','Unlink','Anchor'],
['Style'],
['Table','Image','Flash','Rule','SpecialChar'],
['About']
] ;
And, when you modify the shared Web Part, FCKEditor will appear:
You have only one problem. The file browser for the image and Macromedia Flash files will try to upload your media to the /UserFiles/ folder in your web app. If you use SharePoint, it would be natural to store the media in SharePoint. So, wait for Step 3. Step 3: Custom FileBrowserWe want to store media files to the SharePoint server. This is the quick way.
var _FileBrowserLanguage = 'aspx' ; // asp | aspx | cfm | lasso | perl | php | py
var _QuickUploadLanguage = 'aspx' ;
htmlContentTxt.ImageBrowserURL = "/fckeditor/fileUpload.aspx";
So we need this file. I created fileUpload.aspx page with the code-behind page. I added a
I made a simple solution that puts all the images to the Pics folder in SharePoint. When you select an existing image, the image appears and you can select to add it to your FCKEditor control:
Code for the button click event: sendImage = true;
slika = Image1.ImageUrl;
Or you can upload an image to SharePoint by browsing for an image and clicking the Upload button. Here is the upload button click event: SPWeb sourceWeb;
SPSite sourceSite = SPControl.GetContextSite(Context);
sourceWeb = sourceSite.AllWebs["/"];
sourceWeb.AllowUnsafeUpdates = true;
SPFolder destFolder = sourceWeb.GetFolder("Pics");
if (!destFolder.Exists) destFolder = sourceWeb.Folders.Add("Pics");
try
{
if (File1.PostedFile == null)return;
string strFileName = File1.PostedFile.FileName.Substring(
File1.PostedFile.FileName.LastIndexOf("\\") + 1);
Stream fStream = File1.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
destFolder.Files.Add(strFileName, contents,true);
Response.Write("UploadDocFile :: Success" + strFileName);
sourceWeb.Dispose();
}
catch (Exception ex)
{
Response.Write("UploadDocFile ::" + ex.Message);
}
public Boolean sendImage;
public string slika="", align="", opis="";
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
// {
Image1.Visible = false;
Label1.Visible = false;
Button2.Visible = false;
SPWeb sourceWeb;
List<spdocumentlibrary> docLibs;
SPSite sourceSite = SPControl.GetContextSite(Context);
//new SPSite("http://localhost");
sourceWeb = sourceSite.AllWebs["/"];
sourceWeb.AllowUnsafeUpdates = true;
docLibs = new List<spdocumentlibrary>();
SPFolder destFolder = sourceWeb.GetFolder("Pics");
if (!destFolder.Exists) destFolder = sourceWeb.Folders.Add("Pics");
folderTree.Nodes.Clear();
foreach (SPFile file in destFolder.Files)
{
TreeNode t = new TreeNode();
t.Text = file.Title;
t.Value =sourceSite.Url.ToString()+"/"+ file.Url.ToString();
folderTree.Nodes.Add(t);
}
sourceWeb.Dispose();
}
<% if (sendImage)
{ %>
<script type="text/javascript">
//alert(window.opener.location.href);
var alt="";hspace="";vspace="";width="";height="";
var lresult = new Array();
lresult['src'] = "<%=slika %gt;";
lresult['align'] ="<%=align %>";
//alert(lresult['src']);
lresult['alt'] = "<%=opis %>";
lresult['border'] = "0";
lresult['hspace'] = "5";
lresult['vspace'] = "5";
//parent.returnValue=lresult;
window.opener.SetUrl( "<%=slika %>", 110, 120, "<%=opis %>" );
self.close();
//alert(lresult['src']);
//parent.self.close();
</script>
<% } %>
sendImage = true; slika = Image1.ImageUrl;
Don’t be confused with the variable HistoryIf the
|
||||||||||||||||||||||||||||||||||||||||