Posts Tagged ‘c#’

Client Side Script in a .NET Web Part

Tuesday, August 26th, 2008

Quick, a little snippet on adding client side scripts (javascript) in a web part or ASP.NET page. And the link to MSDN where I found this wonderful information. My script was small, basically an alert to tell the user he/she is stupid. So I embedded the script in the Web Part. It worked. Here is my code snippet, since the one on MSDN was using old function to register the client script.

    private const string ByeByeIncludeScriptKey = "myByeByeIncludeScript";
    private const string EmbeddedScriptFormat =
          "<script language=javascript>alert('Bye Bye');</script> ";

    public WebPart_ClientScript()
    {
       this.PreRender += new EventHandler(WebPart_ClientScript_PreRender);
    }

    private void WebPart_ClientScript_PreRender(object sender , System.EventArgs e )
    {

            if (!Page.ClientScript.IsClientScriptBlockRegistered(ByeByeIncludeScriptKey))
              Page.ClientScript.RegisterClientScriptBlock(typeOf(WebPart_ClientScript), ByeByeIncludeScriptKey, EmbeddedScriptFormat);
    }

Note that James.ToString() says not to use this.GetType but rather typeOf(ControlName) where control name is the web part. So the above just tosses up an alert that happens when the page loads. Although, if you did want a function in there, to have it be called, you add the OnClientClick attribute to the asp tag.

    <asp:Button runat="server" Text="Oh, Click me.." OnClientClick="ShowAlert()" />

And that “ShowAlert()” can be any function name. Simple enough.

SharePoint 2007 Web Part Deployment Tutorial

Monday, August 11th, 2008

There are a bunch of tutorials out there that use C# to create a class library and the put that in the GAC (Windows assemblies folder) etc. I could not get those to work for the life of me. The best way I have found to develop, test, and then deploy a custom SharePoint Web Part to the production server is with Visual Studio SharePoint Extensions, for VS 2005 or VS 2008. Except it seems you can only install this if you have Windows SharePoint Services 3.0 installed. And you can only install WSS 3.0 on Windows Server 2003 SP1. Sorry, no free download for that.

(more…)

Active Directory Password Expiration Email Notifier in C#

Thursday, April 3rd, 2008

At work we have our passwords expire every 90 days, but some people are off-site and do not get the friendly Windows log-on reminders that your password is expiring in, say, 14 days. So my boss thought, “Hey, it would be cool if AD could send out a email to everyone saying their password will expire! Sam, make one!”

So after googling around, I found some posts at CodeProject and some other places I can’t remember, and made a little Windows console app that reads in an XML file for the settings (AD server, email, etc) and sends an email to those whose passwords are expiring in the given number of days. And here is the C# Studio Express Solution…

adpassexpnotifier.zip