Sunday, August 26, 2007

Use your own Code Behind for your Page Layouts or Master Page

You create your Code Behind as normal. You'll need to include the Microsoft.Sharepoint classes that you'll use. Base your Master Page on "System.Web.UI.MasterPage" and your Page Layouts will be "Microsoft.SharePoint.Publishing.PublishingLayoutPage". If you plan to add any controls, do that in the "CreateChildControls()" method. That method is also were you will wire up any events you want for those controls.

Now in your ASPX page, reference the assembly at the top, like:

<%@ Assembly Name"your.assembly.name, version=1.0.0.0, Culture=neutral, Publickeytoken=####" %>

Next add the "Inherits" like:

<%@ Page language="C#" Inherits="your.assembly.name.ClassName"%>

After you build your project, put the DLL in the GAC or the Bin folder of the web application. Then you'll need to reference the DLL in the web.config as a SafeControl.

Now, the page that is using the Page Layout should be using your code behind.

10 comments:

Unknown said...

how can you reference an assembly that is safe from a webcontrol using javascript? Basically, i created a webpart and a WebApplication. The webApp has a few webcontrols and a lot of business logic compiled into one dll. If this was a typical website that ajax calls ( non atlas) would be easy. I am simply using XmlHttp object.

Robert Jones said...

Instead of using AJAX, check out .Net's ICallbackEventHandler.RaiseCallbackEvent and ICallbackEventHandler.GetCallbackResults()

It allows your javascript to interface the server and your dlls.

Menerva said...

i wanna integrate c# script in a sharepoint page like newform.aspx or others, how can you do that?

i'm new in sharepoint and i've a need that i must build it with code.

thanks advance

Robert Jones said...

Menerva: Depends on where the page lives. If it's a page under the /_layouts/ then you need to know that modifying those pages will break your support with MSFT.

One way around this would be to create a HttpModule. You can use this to redirect requests from the /_layouts/ page to your own custom one.

The custom page could be a copy/paste of the original with your own code added.

Menerva said...

My need is to fire a function on a page load.
i tried to use c# script but i was blocked in how to call the function (example :
protected void Page_Load(Sender s, EventArgs E)
{
//--code implemented
}

)

Robert Jones said...

Did you wrap your code with

script runat="server"

code

/script

You just have to create your .CS file and reference it from your page.

Menerva said...

i thought that i just must to write the c# code inline inside the page that i want to customize without the need for a .cs file ?
here's my example :

//script type="text/c#" runat="server"\\
protected void Connect_Click(Object sender, EventArgs e){
SPSite siteCollection = new SPSite("MyApplicationName");
SPWeb site = siteCollection.AllWebs[IndexOfMyWebSite];
foreach(SPList list in site.Lists)
{
Lists.Items.Add(Convert.ToString(list.ID));
}
}
/script

so when i integred this code i've no error displayed, but when i call the function in a button Click event like follow :

input type="submit" id="Connect" value="Get Connect" onclick="Connect_Click" class="ms-vb" /
OR
asp:Button ID="Connect" Text="Get Connect" CssClass="ms-vb" OnClick="Connect_Click" /

it display me an error for calling it, in this step i was blocked.

if you have any suggestion, please let me know.

Robert Jones said...

Try testing with this

//script type="text/c#" runat="server"\\
protected string Foo()
{
return "test1";
}
///script\\

Then in your html

//INPUT TYPE="text" NAME="test1" value="Response.Write(Foo());"\\

I had to strip out some code. Wrap the Response.Write(Foo()) with < % and % >

Menerva said...

Thnx for all Rob, it was very helpfull.before i wrote the server name without Slach so my screen show me different error :D when i fire the event, and suddenly i inspect the code from begin therefor i found the missing Slach.
And with your last code i know now that when calling a function i must wrop it into //% %//.

Menerva said...

hey Rob, i think i found what was the problem, its attached too to the slash missing.
there's an example :

void submit(Object s, EventArgs e)
{
SPSite siteCollection = new SPSite("WebApplicationURL")
SPWeb site = siteCollection.AllWebs[WebSiteIndex];
foreach(SPList list in site.Lists)
{
//ListsItem is a DropDownList Control
ListsItem.Items.Add(Convert.ToString(list.Title));
}
}

// asp:DropDownList ID="ListsItem" runat="server" Width="200px" //

// asp:Button id="button1"
runat="server" OnClick="submit" //

And it works fine.
Now im working on how to fire an event on page load, if you see clearly on it please let me know.