Getting Root Site in Sharepoint


When you are working in sharepoint with extended web applications which is using different authentication methods . You have to need current URL of different domain for hardcoded envoriment . For example http://www.test.com one of your extranet using forms authentication and http://myserver is your default and using windows authentication. if user create a request from http://www.test.com/somedifferet… you need to root of http://www.test.com rather than http://myserver.And you dont want a login screen because of links.

if you type hard code like this its not working for default domain which image not in _layout folder.

<img src=”http://myserver/something/imagenotcomingfromlayout.jpg”&gt; asks extra windows auth login

Solution.

<img src=”/_layouts/GetRootSite.ashx?url=http://myserver/something/imagenotcomingfromlayout.jpg”>

Handlers returns same as users coming request root site.and change http://myserver/ to http://www.test.com

Create a generic handler like below and add this handler to 12/TEMPLATES/LAYOUTS folder for every registered user can reach.

<%@ WebHandler Language=“C#” Class=“GetRootSite” %>

using System;

using System.Web;

using System.Net;


public
class GetRootSite : IHttpHandler

{

public void ProcessRequest (HttpContext context) {

Uri requestUri = context.Request.Url;

string baseUrl2 = context.Request.Url.GetLeftPart(UriPartial.Authority);

string gelenpath = context.Request.QueryString[“url”];

Uri gelen = new Uri(gelenpath);

string gelenPreFix = gelen.GetLeftPart(UriPartial.Authority);

string artakalan = gelenpath.Substring(gelenPreFix.Length);

string sonuc = baseUrl2 + artakalan;

context.Response.Redirect(sonuc);

}

public bool IsReusable {

get {

return false;

}

}

}

Advertisements

About bpostaci
Sr. Support Escalation Engineer in Microsoft.

2 Responses to Getting Root Site in Sharepoint

  1. tan says:

    Hi,
    I have been googling but I can’t find a good start. I am new to Visual Studio 2010.
    I have created an empty SharePoint Project but I can’t Add Item with “Generic Handler”. Where can I do that?
    Thanks.

    • bpostaci says:

      use a class that derived form IHttpHander and register your handler to web.config. you dont need to use ashx file.

      For example

      using System;

      using System.Collections.Generic;

      using System.Linq;

      using System.Text;

      using System.Web;

       

      namespace MyProject.Core.Web.HttpHandlers

      {

          public class FileHandler:IHttpHandler

          {

              public bool IsReusable

              {

                  get { return false; }

              }

       

              public void ProcessRequest(HttpContext context)

              {

                 // Your code is here.

              }

          }

      }

      And add your record in web.config -> httphandlers

         92 <add verb="*" path="file.axd" type="MyProject.Core.Web.HttpHandlers.FileHandler, MyProject.Core" validate="false"/>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: