Wednesday, December 30, 2009

HttpModule and HttpHandler

HttpModule has some events which will be execute every time when the request is processed. But HttpHandler is just executed only on the specific URL.

 

HttpModule has the following events.

·         BeginRequest

·         AuthenticateRequest

·         AuthorizeRequest

·         PreRequestHandlerExecute

·         PostRequestHandlerExecute

·         EndRequest.

 

Web.config entry for HttpModule

<httpModules>
               <add name="CustomHttpModule" type=" MyNameSpace. CustomHttpModule, MyAssembly "/>
</httpModules>

 

Web.config entry for HttpHandler.

<system.web>

<httpHandlers>

               <add verb="*" path="*.raj" type="MyNameSpace.CustomHttpHandler, MyAssembly"/>

</httpHandlers>

</system.web>

 

On the current scenario the httphandler will execute only when the URL has the *.raj request.

 

The HttpHandler should be implemented on IHttpHandler, IHttpAsyncHandler interface of dotnet and HttpModule should be implemented on IHttpModule interface.
 

There is 2 interface in dotnet for synchronous and asynchronous calls of handler the IHttpHandler and  IHttpAsyncHandler

HttpModule and HttpHandler

HttpModule has some events which will be execute every time when the request is processed. But HttpHandler is just executed only on the specific URL.

 

HttpModule has the following events.

·         BeginRequest

·         AuthenticateRequest

·         AuthorizeRequest

·         PreRequestHandlerExecute

·         PostRequestHandlerExecute

·         EndRequest.

 

Web.config entry for HttpModule

<httpModules>
               <add name="CustomHttpModule" type=" MyNameSpace. CustomHttpModule, MyAssembly "/>
</httpModules>

 

Web.config entry for HttpHandler.

<system.web>

<httpHandlers>

               <add verb="*" path="*.raj" type="MyNameSpace.CustomHttpHandler, MyAssembly"/>

</httpHandlers>

</system.web>

 

On the current scenario the httphandler will execute only when the URL has the *.raj request.

 

The HttpHandler should be implemented on IHttpHandler, IHttpAsyncHandler interface of dotnet and HttpModule should be implemented on IHttpModule interface.
 

There is 2 interface in dotnet for synchronous and asynchronous calls of handler the IHttpHandler and  IHttpAsyncHandler

Wednesday, December 23, 2009

What is the meaning of web.AllowUnsafeUpdates

If your code modifies Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, without requiring a security validation. You can do by setting the AllowUnsafeUpdates property.

C#:

using(SPSite mySite = new SPSite(<Server URL>))

 using(SPWeb myWeb = mySite.OpenWeb()) {

myWeb.AllowUnsafeUpdates = true;

SPList interviewList = myWeb.Lists[<ListName>];

SPListItem newItem = interviewList.Items.Add();

newItem[<ColumnName>] = "interview";

newItem.Update(); }  

 

The Difference Between RegisterStartupScript and RegisterClientScriptBlock

We have two different methods for placing JavaScript functions on an ASP.NET page—so what is the difference? The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing </form> element. The RegisterClientScriptBlock method places the JavaScript directly after the opening <form> element in the page. So what difference does this make? It can make quite a bit of difference as we will see.

For an example of this, here is a way to put focus on a text box on a page when the page is loaded into the browser—with Visual Basic .NET using the RegisterStartupScript method:

Page.RegisterStartupScript("Testing", "<script

  language=javascript>document.forms[0]['TextBox1'].focus();</script>")

This works all well and fine because the textbox on the page is generated and placed on the page by the time the browser gets down to the bottom of the page and gets to this little bit of JavaScript. But if instead it was written like this (using the RegisterClientScriptBlock method):

Page.RegisterClientScriptBlock("Testing", "<script

  language=javascript>document.forms[0]['TextBox1'].focus();</script>")

Focus will not get to the textbox control and a JavaScript error will be generated on the page

The reason for this is that the browser will encounter the JavaScript before the text box is on the page. Therefore, the JavaScript will not be able to find a TextBox1.

 

 

Tuesday, October 13, 2009

Sending meeting request through Exchange server API

Here we can send a meeting request through exchange server .

Here is the code for creating the  meeting request.

        ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

        exchangeService.Credentials = new WebCredentials("<user name>", "<password>", "<domain>");

        exchangeService.Url = new Uri("<Exchange server url.>");

 

Here is the method in C#  which creates the meeting request.

public static void CreateMeetingRequest(string ownerEmail, string subjectText, string messageBody, string location, DateTime start, DateTime end, DataTable attendees)

        {

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

                    exchangeService.Credentials = new WebCredentials(EXCHANGE_USER_NAME, EXCHANGE_PASSWORD, EXCHANGE_DOMAIN);

                    exchangeService.Url = new Uri(EXCHANGE_URL);

                    Appointment appointment = new Appointment(exchangeService);

                    appointment.Subject = subjectText;

                    appointment.Body = new MessageBody(BodyType.Text, messageBody);

                    appointment.Start = start;

                    appointment.End = end;

                    appointment.Location = location;

                    appointment.ReminderMinutesBeforeStart = 30;

                    foreach (DataRow drow in attendees.Rows)

                    {

                        appointment.RequiredAttendees.Add(new Attendee(Convert.ToString(drow["Email"])));

                    }

                    appointment.Save();

                });

            }

            catch (ServiceRequestException ex)

            {

                throw new ApplicationException("Error: " + ex.Message);

            }

            catch (WebException ex)

            {

                throw new ApplicationException("Error: " + ex.Message);

            }

        }

Sending meeting request through Exchange server API

Here we can send a meeting request through exchange server .

Here is the code for creating the  meeting request.

        ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

        exchangeService.Credentials = new WebCredentials("<user name>", "<password>", "<domain>");

        exchangeService.Url = new Uri("<Exchange server url.>");

 

Here is the method in C#  which creates the meeting request.

public static void CreateMeetingRequest(string ownerEmail, string subjectText, string messageBody, string location, DateTime start, DateTime end, DataTable attendees)

        {

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

                    exchangeService.Credentials = new WebCredentials(EXCHANGE_USER_NAME, EXCHANGE_PASSWORD, EXCHANGE_DOMAIN);

                    exchangeService.Url = new Uri(EXCHANGE_URL);

                    Appointment appointment = new Appointment(exchangeService);

                    appointment.Subject = subjectText;

                    appointment.Body = new MessageBody(BodyType.Text, messageBody);

                    appointment.Start = start;

                    appointment.End = end;

                    appointment.Location = location;

                    appointment.ReminderMinutesBeforeStart = 30;

                    foreach (DataRow drow in attendees.Rows)

                    {

                        appointment.RequiredAttendees.Add(new Attendee(Convert.ToString(drow["Email"])));

                    }

                    appointment.Save();

                });

            }

            catch (ServiceRequestException ex)

            {

                throw new ApplicationException("Error: " + ex.Message);

            }

            catch (WebException ex)

            {

                throw new ApplicationException("Error: " + ex.Message);

            }

        }

Rename Title field in share point List

Here is the code to rename the Title field. Because the Title is the default field in custom list.


                    SPField f = listCollection[<Guid of List>].Fields["Title"];
                    f.Title = "Name";
                    f.StaticName = "Name";
                    f.Update();



Note: in SPQuery field you should use Title field. because still internal name is Title.

How to insert lookupfield in Sharepoint List.

Here is the code to get the lookup column value and then insert into list.


private static SPFieldLookupValue GetIteminLookUpList(SPWeb webObject, string lookupListName, string lookupString)
{
SPFieldLookupValue fieldLookupValue = null;
try
{
if (CheckListExist(webObject.Lists, lookupListName) == false)
{
// Throw error if the list doesn't exists.
throw new ApplicationException(string.Format("{0}'s list doesn't exists!", lookupListName));
}
SPList lookupList = webObject.Lists[lookupListName];
foreach (SPListItem item in lookupList.Items)
{
if (item["Title"].ToString() == lookupString)
{
fieldLookupValue = new SPFieldLookupValue(item.ID, lookupString);
break;
}
}
return fieldLookupValue;
}
catch (Exception ex)
{
throw new ApplicationException("Error: " + ex.Message);
}
}