X Blogs

who you gonna call?

RadEditor MVC Helper Sample

clock September 9, 2010 00:09 by author Rok Bermež

I was asked for RadEditor MVC helper, so here it goes (copy paste code out) :

        public static MvcHtmlString RadEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
        {

            return RadEditorFor(htmlHelper, expression, ((IDictionary<string, object>)null));
        }

        public static MvcHtmlString RadEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) where TModel : class
        {
            return RadEditorFor(htmlHelper, expression, new RouteValueDictionary(htmlAttributes));
        }

        public static MvcHtmlString RadEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) where TModel : class
        {
            string name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText((LambdaExpression)expression));
            TProperty value = ExpressionHelper2.GetValue(htmlHelper, expression);
            return RadEditor(htmlHelper, name, value, htmlAttributes);
        }

        public static MvcHtmlString RadEditor(this HtmlHelper htmlHelper, string name)
        {
            return RadEditor(htmlHelper, name, null);
        }

        public static MvcHtmlString RadEditor(this HtmlHelper htmlHelper, string name, object value)
        {
            return RadEditor(htmlHelper, name, null, ((IDictionary<string, object>)null));
        }

        public static MvcHtmlString RadEditor(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
        {
            return RadEditor(htmlHelper, name, value, new RouteValueDictionary(htmlAttributes));
        }

        public static MvcHtmlString RadEditor(this HtmlHelper htmlHelper, string name, object value, IDictionary<string, object> htmlAttributes)
        {
            if (String.IsNullOrEmpty(name))
            {
                name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentException("name");
                }
            }
            if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();
            Page page = new Page();
            RadMvcScriptManager mvcScriptManager = new RadMvcScriptManager();
            mvcScriptManager.EnableViewState = false;
            page.Controls.Add(mvcScriptManager);
            RadStyleSheetManager radStyleSheetManager = new RadStyleSheetManager();
            page.Controls.Add(radStyleSheetManager);

            //string id = name.Replace(".", "_");
            RadEditor radEditor = new RadEditor();

            radEditor.ID = name + "_radeditor";
            radEditor.ClientIDMode = ClientIDMode.Static;

            radEditor.ImageManager.ContentProviderTypeName = typeof(AzureStorageFileBrowserContentProvider).AssemblyQualifiedName;
            radEditor.ImageManager.MaxUploadFileSize = 3000000;
            radEditor.ImageManager.ViewPaths = new string[] { "uploaded/images" };
            radEditor.ImageManager.UploadPaths = new string[] { "uploaded/images" };
            radEditor.ImageManager.DeletePaths = new string[] { "uploaded/images" };

            radEditor.DocumentManager.ContentProviderTypeName = typeof(AzureStorageFileBrowserContentProvider).AssemblyQualifiedName;
            radEditor.DocumentManager.MaxUploadFileSize = 20000000;
            radEditor.DocumentManager.ViewPaths = new string[] { "uploaded/documents" };
            radEditor.DocumentManager.UploadPaths = new string[] { "uploaded/documents" };
            radEditor.DocumentManager.DeletePaths = new string[] { "uploaded/documents" };

            radEditor.MediaManager.ContentProviderTypeName = typeof(AzureStorageFileBrowserContentProvider).AssemblyQualifiedName;
            radEditor.MediaManager.MaxUploadFileSize = 50000000;
            radEditor.MediaManager.ViewPaths = new string[] { "uploaded/media" };
            radEditor.MediaManager.UploadPaths = new string[] { "uploaded/media" };
            radEditor.MediaManager.DeletePaths = new string[] { "uploaded/media" };

            radEditor.FlashManager.ContentProviderTypeName = typeof(AzureStorageFileBrowserContentProvider).AssemblyQualifiedName;
            radEditor.FlashManager.MaxUploadFileSize = 50000000;
            radEditor.FlashManager.ViewPaths = new string[] { "uploaded/flash" };
            radEditor.FlashManager.UploadPaths = new string[] { "uploaded/flash" };
            radEditor.FlashManager.DeletePaths = new string[] { "uploaded/flash" };

            //radEditor.TemplateManager.ContentProviderTypeName = typeof(AzureStorageFileBrowserContentProvider).AssemblyQualifiedName;
            radEditor.TemplateManager.MaxUploadFileSize = 500000;
            radEditor.TemplateManager.ViewPaths = new string[] { "~/content/templates" };

            radEditor.NewLineBr = false;

            radEditor.Skin = "Vista";

            //radEditor.ToolsFile = "~/Content/administracija/EditorToolbar.xml";
            if (htmlAttributes.ContainsKey("ToolsFile")) radEditor.ToolsFile = htmlAttributes["ToolsFile"] as string;
            UIHintControlParameterDataAnnotationsModelMetadata mmd = (UIHintControlParameterDataAnnotationsModelMetadata)htmlHelper.ViewData.ModelMetadata;
            if (mmd.TemplateControlParameterExist("ToolBar"))
            {
                radEditor.ToolsFile = mmd.GetTemplateControlParameter<string>("ToolBar");
            }
            if (mmd.TemplateControlParameterExist("DisableHtmlFormating") && mmd.GetTemplateControlParameter<bool>("DisableHtmlFormating"))
            {
                radEditor.StripFormattingOptions = EditorStripFormattingOptions.None;
                radEditor.DisableFilter(Telerik.Web.UI.EditorFilters.ConvertToXhtml);

            }
            if (value != null) radEditor.Content = value as string;
            radEditor.DialogHandlerUrl = "/Telerik.Web.UI.DialogHandler.axd";
            page.Controls.Add(radEditor);
            StringWriter output = new StringWriter();
            HttpContext.Current.Server.Execute(page, output, false);

            return MvcHtmlString.Create(htmlHelper.Hidden("", "ToBeReplaced").ToString() + output.ToString());
        }


Telerik ASP.NET AJAX Controls in MVC project Part ||

clock September 4, 2010 00:09 by author Rok Bermež

Some controls explicitly require to be placed within

tag. To go around this issue you can do something like this (telerik reporting used in MVC project):
public class NoServerFormPage: Page
        {
            public override void VerifyRenderingInServerForm(Control control)
            {
                // to avoid the server form (<form runat="server">) requirement
            }
        }
        
        public static MvcHtmlString TelerikReport(this HtmlHelper htmlHelper, object  value, object htmlAttributes )
        {
            NoServerFormPage page = new NoServerFormPage();
            RadMvcScriptManager mvcScriptManager = new RadMvcScriptManager();
            mvcScriptManager.EnableViewState = false;
            page.Controls.Add(mvcScriptManager);
            RadStyleSheetManager radStyleSheetManager = new RadStyleSheetManager();
            page.Controls.Add(radStyleSheetManager);

            ReportViewer reportViewer = new ReportViewer();
            reportViewer.ID = "whoCares";
            reportViewer.Height = 890;
            reportViewer.Width = 700;
            
            reportViewer.ToolbarVisible = true;
            reportViewer.Report = new Ntk.Reports.Racun();
            ((Telerik.Reporting.Report)reportViewer.Report).ReportParameters["ID"].Value = value;
            reportViewer.Resources.CurrentPageToolTip = "Trenutna stran";
            reportViewer.Resources.ExportButtonText = "Izvozi";
            reportViewer.Resources.ExportSelectFormatText = "Izberi format za izvoz";
            reportViewer.Resources.ExportToolTip = "Izvoz";
            reportViewer.Resources.FirstPageToolTip = "prva stran";
            reportViewer.Resources.LabelOf = "od";
            reportViewer.Resources.LastPageToolTip = "Zadnja stran";
            reportViewer.Resources.NextPageToolTip = "naslednja stran";
            reportViewer.Resources.PreviousPageToolTip = "prejšnja stran";
            reportViewer.Resources.PrintToolTip = "Natisni";
            reportViewer.Resources.ProcessingReportMessage = "Nalagam...";
            reportViewer.Resources.RefreshToolTip = "Osveži";
            

            page.Controls.Add(reportViewer);
            StringWriter output = new StringWriter();
            HttpContext.Current.Server.Execute(page, output, false);

            return MvcHtmlString.Create(output.ToString());
        }


Battle with User Profile Service

clock September 3, 2010 23:32 by author Robi

I was configuring User Profile Service at one of our clients, and the problem was that after provisioning User Profile Service I was getting 6801 and 6803  errors in event log.

 

Central Administration site was using SSL, for example https://sharePointServer

Following examples at http://www.mylifeinaminute.com/2010/07/07/sharepoint-2010-user-profile-synchronization-errors/ I tried to fix things, but it didn't work for me...

So, after using ULS Viewer and miisclient (C:\Program Files\Microsoft Office Servers\14.0\Synchronization Service\UIShell\miisclient.exe) I found out that User profile service can not connect to the approprite Web Service.

What I did was, I opened up a miisClient, opened Management Agents, right clicked on MOSS{guid} Extensible connectivity, clicked on Properties and Configure Connection Information. After trying to configure the right connection, I found out that even though Central Administration site is on URL https://sharepointserver, the user profile connection instead of connecting to direct://sharepointserver/_vti_bin/ProfileImportExportService.asmx?ApplicationID=27c26eca%2D6eeb%2D4da4%2D9012%2Da51e02baae9a, I entered:https://SharePointServer.xnet.si/_vti_bin/ProfileImportExportService.asmx?ApplicationID=27c26eca%2D6eeb%2D4da4%2D9012%2Da51e02baae9a and everything started to work normaly.

 

I hope that helps

 



Telerik ASP.NET AJAX Controls in MVC project

clock August 18, 2010 23:37 by author Rok Bermež

If we want to use Telerik ASP.NET AJAX controls in MVC project, we must also use either ScriptManager or RadScriptManager. But since neither works very well here is more MVC friendly solution:

 public class RadMvcScriptManager : RadScriptManager
{
static string JavaScriptBlockFormat = @"<script type=""text/javascript"">
//<![CDATA[
{0}
//]]>
</script>"
;

protected override void Render(HtmlTextWriter writer)
{
foreach (RegisteredScript script in GetRegisteredClientScriptBlocks())
{
if (HttpContext.Current.Items[script.Key] == null)
{
HttpContext.Current.Items[script.Key] = true;

if (script.ScriptType == RegisteredScriptType.ClientScriptInclude)
{
writer.WriteLine(String.Format(@"<script type=""text/javascript"" src=""{0}""></script>",
HttpUtility.HtmlEncode(script.Url)));
}
}
}



if (HttpContext.Current.Items["AppInitialize"] == null)
{
Page.Items["AppInitialize"] = true;
writer.WriteLine(
String.Format(
JavaScriptBlockFormat,
@"if(typeof(Sys) != ""undefined""){
$addHandler(window, "
"load"", function(){Sys.Application.initialize();}); }
else { throw new Error("
"Microsoft ASP.NET AJAX cannot be initialized!"")}"));
}

StringBuilder builder = new StringBuilder();

if (HasReflectionPermission())
{
SerializeScriptsForScriptControls(Page, builder);
}

string scriptContent = builder.ToString();
if (!String.IsNullOrEmpty(scriptContent))
{
writer.WriteLine(String.Format(
JavaScriptBlockFormat,
String.Format("Sys.Application.add_init(function(){{{0}}});", scriptContent)));
}
}

internal void SerializeScriptsForScriptControls(Control control, StringBuilder builder)
{
if (control is IScriptControl && control.Visible)
{
IEnumerable<ScriptDescriptor> descriptors = ((IScriptControl)control).GetScriptDescriptors();
if (descriptors != null)
{
foreach (ScriptDescriptor descriptor in descriptors)
{
SerializeScriptControlDescriptor(descriptor, builder);
}
}
}

if (control.HasControls())
{
foreach (Control child in control.Controls)
{
SerializeScriptsForScriptControls(child, builder);
}
}
}

internal void SerializeScriptControlDescriptor(ScriptDescriptor descriptor, StringBuilder builder)
{
MethodInfo getScriptInfo = descriptor.GetType().GetMethod(
"GetScript",
BindingFlags.Instance | BindingFlags.NonPublic);
if (getScriptInfo != null)
{
string script = (string)getScriptInfo.Invoke(descriptor, new object[] { });
builder.AppendLine(script);
}
}

internal static bool HasReflectionPermission()
{
try
{
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
return true;
}
catch (SecurityException)
{
return false;
}
}
}


SharePoint 2010 Servicve Accounts and Windows Server 2008 R2

clock July 23, 2010 18:16 by author Robi

Yesterday, I was was installing SharePoint Server 2010 at one of our clients and after the install I was getting this errors:

User Profile Service Event ID 1511. It says that account could not log on to the computer and create a profile.

After a little bit of research, I found out that all you have to do is edit a local group policy and than run a cmd and all of these errors are gone.

So run a "gpedit.msc" on your WFE where you are getting these errors and go to Computer Configuration/Windows Settings/Security Settings/Local Policies/User Rights Assigneements and Search for Allow log on Locally

Add the account for which you are getting errors. Then, start command prompt and run a command:

 runas /user:kompas-xnet\sp_apppool cmd

 Type the password and if you go to your users folder you should see a folder with your service account name.

You can now remove Allow log on locally permission for the account. 

Voila.

 



Handy jQuery developer plugins

clock July 6, 2010 21:32 by author Klemen Slavič

I've commited a new plugin to my open source repository at Google Code – Niftyscripts. Currently, I've pushed a new plugin that scans the current document and reports unused CSS rules and their origins.

To read more, visit the site or browse the source tree.



Photoshop Tutorial: Shiny Apple Icons

clock July 4, 2010 06:08 by author Klemen Slavič

You can read my new tutorial in Slovene here.



Stop or start all SharePoint 2010 Services

clock July 3, 2010 00:37 by author Robi

During a maintainance of my computer I needed to stop all SharePoint 2010 services. So I was wondering what is the easiest way to do it. In Central Admin, you could do it an click on every service and stop it, but it takes a lot of time. So I was considering some other options and certainly decided that you could do it with a PowerShell script. So here it is:

STOP

$services=Get-SPServiceInstance
foreach ($service in $services) {stop-SPServiceInstance -Identity $service -confirm:$false}

START

$services=Get-SPServiceInstance
foreach ($service in $services) {start-SPServiceInstance -Identity $service}

 



Dealing with Error 404 on IIS7 using ASP.NET Routing

clock June 15, 2010 18:06 by author Klemen Slavič

When you're deploying an ASP.NET website that uses the new Routing feature from MVC, you might run into a problem mapping the routes in Integrated mode. The documentation states that using Integrated mode on IIS7 doesn't entail any extra configuration, but in the odd case it does (throwing Error 404 on routed URLs but not .aspx paths), you can open up your web.config file and modify your <system.webServer> / <modules> tag:

<modules>
...
</modules>

... to something like this:

<modules runAllManagedModulesForAllRequests="true">
...
</modules>

Or, if you're not using modules, just put an empty tag within <system.webServer>:

<modules runAllManagedModulesForAllRequests="true" />

That forces IIS7 to always run all loaded web server modules, including URL Routing (inherited from the 4.0 application pool).



Create Custom Site Definition

clock June 10, 2010 00:02 by author Robi

Hi,

In this post I'm going to explain how to create custom site definition in SharePoint 2010.

First of all, you can copy or create your own custom site definition in this folder and rename it to what ever you like:

 

Next, you have to create a XML file, where you define which template to use, what the title o your definition is going to be and which icon you are going to use. in the next 2 pictures you are going to see the path and the code you have to insert in this XML file.

when you are done, you have to do an IISRESET and then you are good to go with your custom Site Definition.

 

 



About the author

Rok Bermež is Slovenian Windows Azure MVP, he works as a Software Engineer and Microsoft Certified Trainer at Kompas Xnet. His primary interests include cloud computing and web development. With extensive experience in development and architecture he participated in many projects and since the CTP release of Windows Azure much of those projects are based on Windows Azure platform. Rok has been delivering courses, writing code and speaking at conferences and community events on Microsoft technologies for the last couple of years. You can also find him on Twitter (@Rok_B).

Month List

Sign In