Thursday, October 1, 2009

Simplest way to Call server Side funtion from Client side

Step 1: Drop a Script Manager in Page and set its EnablePageMethods property to True.

Step 2: Create a function on the server side that needs to be called from Client Side and add the attribute [WebMethod] on top of it.
[System.Web.Services.WebMethod]
public static void SaveInfo(string Id)
{
// Do server side coding……

}
Setp 3: In the Client Side function just call it like.


function makeFavorite(id) {
PageMethods.SaveInfo(id, CallSuccess, CallFailed);
}

// This will be Called on success
function CallSuccess(res, id) {
alert(destCtrl);
}

// This will be Called on failure
function CallFailed(res) {
alert(res.get_message());
}

Wednesday, September 2, 2009

Simplest Way to Debug a windows Service

Step 1: Create a windows Service.
Setp 2: Open Program.cs
Step 3: Comment the main function
Step 4: Add the Following in ur main function

static void Main()
#if (!DEBUG) System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

#else
// Name of ur Service
Service1 service = new Service1();
// The Function you want to call on start
service.XYZ(); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}




Happy Debugging...

Source: Check Full Code here

Thursday, July 16, 2009

Regular expression for HTML anchor Tag

hi,
I was in search of finding a regular expression that matches the anchor tag in HTML and updates the value of the href attributes.

i have gone through many of them but none supports the anchor tag in which the qoutes around href are not present i.e.

so here in the updated Regular expression that matches all kinds of anchor tag and then a small chunk of c# code to get and update the value of href attribute.


public string UpdateHTMLAnchorTag(string input)
{
string initialURL = PLACE THE ABOVE REGULAR EXPRESSION HERE
Regex regex = new Regex(initialURL, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(input);
string ma = string.Empty;
foreach (Match m in matches)
{
ma = m.Result("${url}");
input = input.Replace(tempMa, "UpdatedMA");


}
return input;

}



Wednesday, July 8, 2009

Events in Community server 2008.5

I was assigned a task to write Events module in community server 2008.5, Many ways i looked for but dint find any good one to get ma work done, I decided to make a complete custom module for this purpose but again searching, Tags , blah blah ... y to make whats already done so i decided to modify a blog post and make it look like event.

lets begin what i did.

first of all events sud have some customized fields like location, Map url , start date, start time.
simply go to the community server Database and make table for all the customized fields.
just one this you need to append to it is a POST Id from cs_posts table. so that we can use the basic post functionality with events.

Okay now just goto the control pannel make a new blog. One important this "DONOT FORGET TO MAKE A NEW GROUP FOR THE BLOG". when you are dont with this just goto the CS code. navigate to folder Controlpannel> blogs > PostEditor.aspx
this is where we need to place the Extended module .

In order to make an extended module create a Class library project. Import all the DLL from the bin directory of CS Code.

now we need to create a SUB FORM but this time we wont be extending a user registration module we will be extending Blog post module.

========================================================================

using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.Controls;
using CommunityServer.Components;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using System.Data.SqlClient;
using System.Data;
using CommunityServer.Blogs.Controls;
using System.Threading;
using System.Globalization;
//using CommunityServer.Blogs.Services;

namespace CSCustom
{
public class WeblogAdminEventSubForm : WrappedSubFormBase
{
// storage for the reference to the DropDownList containing car brands
TextBox EventLocation;
TextBox EventStartDate;
TextBox EventEndDate;




public string EventLocationId
{
get { return (string)(ViewState["EventLocationId"] ?? ""); }
set { ViewState["EventLocationId"] = value; }
}

public string EventStartDateId
{
get { return (string)(ViewState["EventStartDateId"] ?? ""); }
set { ViewState["EventStartDateId"] = value; }
}

public string EventEndDateId
{
get { return (string)(ViewState["EventEndDateId"] ?? ""); }
set { ViewState["EventEndDateId"] = value; }
}



public override bool IsEnabled()
{
// permissions can also be checked here.

return true;
}

protected override void AttachChildControls()
{
// find the DropDownList that will contain car brands
this.EventLocation = CSControlUtility.Instance().FindControl(this, this.EventLocationId) as TextBox;
this.EventStartDate = CSControlUtility.Instance().FindControl(this, this.EventStartDateId) as TextBox;
this.EventEndDate = CSControlUtility.Instance().FindControl(this, this.EventEndDateId) as TextBox;


// if the DropDownList couldn't be found, throw an exception
if (this.EventLocation == null || this.EventEndDate == null || this.EventStartDate == null)
throw new InvalidOperationException("The Event Details must identify a valid Textboxes");

// retrieve the current selected value
string startDate = this.EventStartDate.Text;
string endDate = this.EventEndDate.Text;
string location = this.EventLocation.Text;



// attempt to re-set the selected value
if (!string.IsNullOrEmpty(startDate))
EventStartDate.Text = startDate;

if (!string.IsNullOrEmpty(endDate))
EventEndDate.Text = endDate;

if (!string.IsNullOrEmpty(location))
EventLocation.Text = location;

}

public override void DataBind()
{
// call the base DataBind method -- it enabled DisplayConditions and other Chameleon features
base.DataBind();

// the DataSource is automatically set to the host form's DataSource
if (this.DataSource != null)
{
WeblogPostData wep = this.DataSource as WeblogPostData;
Post p = this.DataSource as Post;
// Class Custom Function will define all the Functions required in Custom modules
CS cs = new CS();
cs.PostId = p.PostID;
cs.getPostByPostId(cs);

if (string.IsNullOrEmpty(EventLocation.Text))
{
EventLocation.Text = cs.Location;
}
if (string.IsNullOrEmpty(EventStartDate.Text))
EventStartDate.Text = cs.StartDate.ToString("MM/dd/yyyy");
if (string.IsNullOrEmpty(EventEndDate.Text))
EventEndDate.Text = cs.EndDate.ToString("MM/dd/yyyy");

}
}

///
/// This Event will fire before the parent form is submitted

public override void ApplyChangesBeforeCommit(object activeObject)
{
base.ApplyChangesBeforeCommit(activeObject);

}


public override void ApplyChangesAfterCommit(object activeObject)
{
base.ApplyChangesAfterCommit(activeObject);

Post p = (Post)activeObject;
CS.insertEventDetails(p.PostID, EventStartDate.Text,
EventEndDate.Text, EventLocation.Text);
}
}
}




========================================================================

In the Above subform code you will be finding a CS class this is the custom class added in the community server class library project. In this class we will just interact with our database to fetch the Events details.

Now the Events module admin is there its time to change the PostEditor page to show updated fields only for events.





=========================================================================

<csblog:createeditweblogpostform runat="server" id="CreatePost" commentmoderationdropdownlistid="ModerationDDL" customvalidatorid="PostValidator" enableallownernotificationcheckboxid="ynNotifyAllOwners" enablecrosspostingcheckboxid="ynEnableCrossPosting" enableratingscheckboxid="ynEnableRatings" enabletrackbackscheckboxid="ynEnableTrackbacks" feedbacknotificationdropdownlistid="NotificationType" isaggregatedcheckboxid="ynAggregatePost" iscommunityaggregatedcheckboxid="ynCommunity" enablecommentscheckboxid="ynEnableReplies" ispublishedcheckboxid="Publish" postbodyeditorid="PostBody" postdatedatetimeselectorid="DatePicker" postexcerpteditorid="postExcerpt" postnametextboxid="postName" postsubjecttextboxid="PostSubject" previewweblogpostlistid="PreviewList" subformids="PostTags,PostAttachment,PostVideo,EventAttach" submitbuttonid="PostButton" syndicateexcerptcheckboxid="ynSyndicateExcerpt">
<successactions>
<cscontrol:customaction runat="server" oncustomevent="PostSaved">
</cscontrol:customaction>
<formtemplate>



</formtemplate>
</successactions>


=========================================================================

In the Code below their is a control CreateEditWeblogPostForm , in its form templates tag add the new subfrom that we just made. and assign its ID to Subforms ID attribute.

=========================================================================
<textarea style="width:400px; height:600px">
<cscustom:weblogadmineventsubform id="EventAttach" runat="server" eventlocationid="txtLocation" eventstartdateid="txtStartDate" eventenddateid="txtEndDate" eventurlid="txtURL" opennewid="chkNewWindow" eventtimeid="txtTime" eventvenueid="txtVenue">
<displayconditions>
<csblog:grouppropertyvaluecomparison id="GroupPropertyValueComparison1" comparisonproperty="GroupId" runat="server" operator="EqualTo" comparisonvalue="16">
</csblog:grouppropertyvaluecomparison>
</displayconditions>
<formtemplate>
<p>
</p><div class="CommonFormFieldName">
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr valign="top" height="30">
<td><strong>
<cp:formlabel id="Formlabel8" runat="Server" controltolabel="txtLocation" resourcename="Weblog_CreateEditBlogPost_Location"></cp:formlabel></strong>
<asp:textbox columns="50" id="txtLocation" runat="server">

<asp:requiredfieldvalidator id="rfvLocation" controltovalidate="txtLocation" runat="server" errormessage="Location is Required"></asp:requiredfieldvalidator>
</asp:textbox></td>
</tr>

<tr valign="top" height="30">
<td>
<strong>
<cp:formlabel id="Formlabel9" runat="Server" controltolabel="txtStartDate" resourcename="Weblog_CreateEditBlogPost_StartDate"></cp:formlabel></strong>
<asp:textbox columns="50" id="txtStartDate" runat="server">
<asp:regularexpressionvalidator id="revStartDate" controltovalidate="txtStartDate" validationexpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" runat="server" errormessage="mm/dd/yyyy"></asp:regularexpressionvalidator>
<asp:requiredfieldvalidator id="rfvStartDate" controltovalidate="txtStartDate" runat="server" errormessage="Start Date is Required"></asp:requiredfieldvalidator>
</asp:textbox></td>
</tr>
<tr valign="top" height="30">
<td>
<strong>
<cp:formlabel id="Formlabel10" runat="Server" controltolabel="txtEndDate" resourcename="Weblog_CreateEditBlogPost_EndDate"></cp:formlabel></strong>
<asp:textbox columns="50" id="txtEndDate" runat="server">
<asp:regularexpressionvalidator id="revEndDate" controltovalidate="txtEndDate" validationexpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" runat="server" errormessage="mm/dd/yyyy"></asp:regularexpressionvalidator>
<asp:requiredfieldvalidator id="rfvEndDate" controltovalidate="txtEndDate" runat="server" errormessage="End Date is Required"></asp:requiredfieldvalidator>
</asp:textbox></td>
</tr>
<tr valign="top" height="30">
<td>
<strong>
<cp:formlabel id="Formlabel13" runat="Server" controltolabel="txtEndDate" resourcename="Weblog_CreateEditBlogPost_EndDate"></cp:formlabel></strong>
<asp:textbox columns="50" id="txtURL" runat="server">

</asp:textbox></td>
</tr>
<tr valign="top" height="30">
<td>
<strong>
<cp:formlabel id="Formlabel14" runat="Server" controltolabel="txtEndDate" resourcename="Weblog_CreateEditBlogPost_EndDate"></cp:formlabel></strong>
<asp:checkbox id="chkNewWindow" runat="server"></asp:checkbox>
</td>
</tr>
</tbody></table>
</div>
</formtemplate>
</cscustom:weblogadmineventsubform>

===========================================================================

In tyhe above code you will find a value, Where group ID is set to "16" this 16 is the Id of the group which was assigned to BLog that we made for events.




<csblog:grouppropertyvaluecomparison id="GroupPropertyValueComparison1" comparisonproperty="GroupId" runat="server" operator="EqualTo" comparisonvalue="16">




this is all for the Admin Side.

When you will be opening the Blog Post for events extra fields will be visible to you that will be saved in your own table.


I will soon be writing about the showing event to the Community server users.

Hope this Blog helped ya...

Cheers.
Usman Shabbir