19 November, 2012

Null reference in EPiServer filemanager

You get the following error when trying to open a folder in the EPiServer file manager.


[NullReferenceException: Object reference not set to an instance of an object.]
EPiServer.Web.Hosting.Versioning.VersioningFileSummary..ctor(FileItem file, FileOperations fileOp)

This might happen if the file on disk is removed but the reference to it is still in the EPiServer database. It can also happen if the versioning data has become corrupt for some reason.

I solved it by creating the following simple page/tool:

ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FixFiles.aspx.cs" Inherits="QD.Web.FixFiles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Deleting files where source don't exist" />
        <asp:Label ID="StartIndexOnItemLit" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Code behind
using System;
using System.Text;
using System.Web.Hosting;
using System.Web.UI;
using EPiServer.Web.Hosting;

namespace QD.Web
{
    public partial class FixFiles : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)

        {
            StartIndexOnItemLit.Text = "";
            var builder = new StringBuilder();
            VirtualPathHandler instance = VirtualPathHandler.Instance;


            foreach (VirtualPathProvider a in VirtualPathHandler.Instance.VirtualPathProviders.Keys)
            {
                if (a is VirtualPathUnifiedProvider)
                {
                    VirtualDirectory dir = instance.GetDirectory((a as VirtualPathUnifiedProvider).VirtualPathRoot, true);

                    foreach (object element in dir.Children)
                    {
                        CheckItem(builder, element);
                    }
                }
            }


            StartIndexOnItemLit.Text = "<ul>" + builder + "</ul>";

        }


        private void CheckItem(StringBuilder builder, object element)

        {
            if (element is UnifiedDirectory)
            {
                CheckDir((element as UnifiedDirectory), builder);
            }

            if (element is UnifiedFile)
            {
                CheckFil((element as UnifiedFile), builder);
            }
        }

        private void CheckDir(UnifiedDirectory dir, StringBuilder builder)
        {
            try
            {
                builder.AppendLine("<ul>");

                foreach (object a in dir.Children)
                {
                    CheckItem(builder, a);
                }

                builder.AppendLine("</ul>");
            }

            catch (Exception error)

            {
                builder.AppendLine(dir.Name + " " + dir.VirtualPath + " " + error.Message + " " + error.StackTrace);
            }
        }

        private void CheckFil(UnifiedFile fil, StringBuilder builder)
        {
            try
            {
                IUnifiedSummary a = fil.Summary;
            }
            catch (Exception error)
            {
                builder.AppendLine("<li>deleting file since source don't exists " + fil.VirtualPath + "</li>");

                fil.Delete();
            }
        }
    }
}


15 November, 2012

Sys.UI is undefined when using Component Art


I had a hard time finding out why i got the "Sys.UI is undefined" error on a page.

After some trouble shooting I found out that the problem was that I use Component Art to render a calendar on my page and the Coponent Art dll was for .net 3 not .net 4 which was the version of my project.

So the solution was to download the new version from Component Art, install it and then reference to the new version in my project.

12 October, 2012

Relate+ or community search - Could not get search results for uri




I had a hard time finding out why the search service in EPiServer community (or relate+) did not return any search results. When investigating the EPiServer log (log4net) I found that the webserver returned 404 (not found) when trying to send the search request to the search service IndexingService.svc.

But it was possible to reach the service by entering its url in a web browser. (I got the "endpoint not found" error which means that I reached the service)

So it seemed crazy that it didn't work. It really should work (heard that before?)

I than analyzed the IIS-logs and found out that the 404 error that was reported in the EPiServer log really was an 404.15 error (Query String Too Long).

So I analyzed what was really sent to the search service, and found out that all groups that the user is a member of was sent in the request. Since we use AD-accounts the users were members of lots of groups and the query string became very long.

I solved it by adding the following to the generic system.webServer section of web.config.


<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxQueryString="10000" />
        </requestFiltering>
    </security>
</system.webServer>

Here is the error message I found in the EPiServer log


EPiServer.Search.RequestHandler.GetSearchResults - Could not get search results for uri 'http://{your site}/IndexingService/IndexingService.svc/search/?q=(EPISERVER_SEARCH_DEFAULT%3a(searchword*))+AND+(EPISERVER_SEARCH_ACL%3a(U%5c%3a415aa516%5c-1653%5c-46b0%5c-85c0%5c-2ee3faf5e08d)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+1b6bafd0%5c-44c4%5c-4b13%5c-bf29%5c-fe110c7c1110)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+21adf78a%5c-aba5%5c-452a%5c-9ebf%5c-c5cd345abc7e)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+5ee681c7%5c-f001%5c-4478%5c-a7b3%5c-62e6f6cca6a3)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+6ab59e8a%5c-47d6%5c-4d17%5c-aa83%5c-6bc3e0fba1ac)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+95ed85c7%5c-8982%5c-4b13%5c-913b%5c-967a10b9edf5)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+ac01b6a8%5c-a740%5c-4d69%5c-a523%5c-029133dd41c7)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+bc60d25e%5c-f254%5c-43d5%5c-93ef%5c-32527e5f849c)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+c2c78b48%5c-bc99%5c-4226%5c-b377%5c-86398f105ed1)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+cb229ec7%5c-8d31%5c-4000%5c-807c%5c-491fbc9e34fd)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+de2a5931%5c-5c58%5c-4fe6%5c-a183%5c-4f24d9f5dead)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+f6b18ceb%5c-eee8%5c-4b1a%5c-9248%5c-9eac2c759d56)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+fd39c628%5c-40eb%5c-4599%5c-b972%5c-da9dfda5b7d5)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aClub+members+fd984453%5c-54f0%5c-4daa%5c-aa3d%5c-9e9b6125b387)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aCommunityMembers)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aEveryone)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAuthenticated+Users)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aNETWORK)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aNTLM+Authentication)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aThis+Organization)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+AgdaEntre)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+Allow+Client+Removable+Drives)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+MailBox_Info)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+Microsoft+Office+2010)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+Microsoft+Outlook+2010)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+Microsoft+Snipping+Tool)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+yourcompanyIntra)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+yourcompanyIntra+Admins)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+yourcompanyIntra+Editors)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+yourcompanyIntra+Upload)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+XenApp+Desktop)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG+XenApp+OTP)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aAG_XenApp_VPN)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aDomain+Users)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-010)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-060)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-060%5c-Diego)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-080)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-130)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-130%5c-Projekt+G)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-130%5c-Projekt+G%5c-01)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-150%5c-GIAB_Info_diverse)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-200+SAP%5c-W)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aG%5c-IntranetAdmin)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3ayourcompany+HK)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3ayourcompany+Users)+OR+EPISERVER_SEARCH_ACL%3a(G%5c%3aUsers))&namedindexes=&offset=0&limit=10&format=xml&accesskey=local'. Message: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse()


11 October, 2012

This collection already contains an address with scheme http


When using EPiServer Community (relate+) on a site with several configured host header names the search stops working.

When trying to reach the search service through the browser (http://{my site}/IndexingService/IndexingService.svc/) I get the  "This collection already contains an address with scheme http" error

I fixed this issue by adding multipleSiteBindingsEnabled="true" to the serviceHostingEnvironment tag in web.config (it's locatated in the serviceModel tag)

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" >

02 October, 2012

Check if the page is in editmode in EPiServer 7


The official way to check if the page is in edit mode in EPiServer 7 is:
PageEditing.PageIsInEditMode


Another way might be to check the request parameter:
if (! string.IsNullOrEmpty(Request.Params["epieditmode"]))

      return true; 

04 September, 2012

Add and remove values in User Profile criteria


I was asked by a customer to add "Country" to User Profile criteria as it was not in the dropdown list.

User Profile dropdown list














I haven't used Visitor Groups before and was a bit surprised that that the values in the dropdown list were shorter than expected. I thought the list was populated from Personalization in EPiServer and could be configured in SqlProfile in web.config. 

profile section i web.config














Just to be sure I added a value to SqlProfile and it appeared in the dropdown list after a reload. So my first thought was right, but what had happened with the removed values? I now suspected that EPiServer for some reason or another had chosen to filter the list. Loading up episerver.dll in dotPeek revealed the truth. In the initializing code the class added an ignore list and the missing values where added to the ignore list. The next question was how to enable those values. After contact with EPiServer support they provided me with an easy way to control the ignore list.

The way to do it is to call the criterion in global.asax.cs, I used application_start. Here are some examples:

1. To remove all values in the list, e.g. show all values:
protected void Application_Start(Object sender, EventArgs e)
{          EPiServer.Personalization.VisitorGroups.Criteria.UserProfileCriterion.IgnoreUserProfileProperties.Clear();
        }

2. To remove a specific value, e.g. show "Country":
protected void Application_Start(Object sender, EventArgs e)
{            EPiServer.Personalization.VisitorGroups.Criteria.UserProfileCriterion.IgnoreUserProfileProperties.Remove("Country");
        }

3. To add a specific value, e.g. hide "FirstName":
protected void Application_Start(Object sender, EventArgs e)
{            EPiServer.Personalization.VisitorGroups.Criteria.UserProfileCriterion.IgnoreUserProfileProperties.Add("FirstName");
        }
This should be useful for hiding the values that make no sense for creating Visitor Groups, just as EPiServer did with their default values. Although I think they probably removed some useful values, like "Country".

The complete list of default properties are:

    IgnoreUserProfileProperties.Add("SubscriptionInfo");
    IgnoreUserProfileProperties.Add("CustomExplorerTreePanel");
    IgnoreUserProfileProperties.Add("FileManagerFavourites");
    IgnoreUserProfileProperties.Add("EditTreeSettings");
    IgnoreUserProfileProperties.Add("ClientToolsActivationKey");
    IgnoreUserProfileProperties.Add("FrameworkName");
    IgnoreUserProfileProperties.Add("ZipCode");
    IgnoreUserProfileProperties.Add("Locality");
    IgnoreUserProfileProperties.Add("Language");
    IgnoreUserProfileProperties.Add("Country");

22 August, 2012

.Net and HTML 4.01


It's not that easy to make .Net produce valid HTML 4.01.

One of the problems is that even if you write <br> in your webform .Net will "correct" it to <br /> before the html-stream is sent to the webserver. (it's the same with all "not closed" html-tags)

Another problem is that .Net adds tags for Viewstate that has ID's that not conform to the HTML 4.01 standard.(__VIEWSTATE and __EVENTVALIDATION)

This was an terrible issue for me since I have a customer that requires HTML 4.01 that validates to the W3C validator.

After some massive googling I found an article with a suggestion on how to manipulate the html-stream before it reaches the webserver. The article is written by Rick Strahl and can be found here: Capturing and Transforming ASP.NET Output with Response.Filter

This is done by creating a response filter.
The code for the filter can be found here

I implemented it in the code behind of my master page like this:


protected override void OnInit(System.EventArgs e)
{
    base.OnInit(e);
    //To remove warning about Byte Order Mark
    Response.ContentEncoding = new System.Text.UTF8Encoding(false);

    ResponseFilterStream filter = new ResponseFilterStream(Response.Filter);
    filter.TransformString += filter_TransformString;
    Response.Filter = filter; 
}

string filter_TransformString(string output)
{
    return MakeHTML4_01(output);
}

/// <summary>
/// Tries to adjust output to conform to HTML4.01 standard
/// </summary>
/// <param name="output">The output adjusted to conform to HTML4.01 standard</param>
/// <returns></returns>
private string MakeHTML4_01(string output)
{
    output = output.Replace("id=\"__VIEWSTATE\"""id=\"VIEWSTATE\"");
    output = output.Replace("id=\"__EVENTVALIDATION\"""id=\"EVENTVALIDATION\"");
    return output.Replace("/>"">");
}


(Off course it would be faster to use regexp to do the replacements, but I wanted the example to be clean.)