Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, 21 May 2011

ModuleSettings and TabModuleSettings Difference in DotNetNuke

If you do not like to read preambula and want to know what is the difference between ModuleSettings and TabModuleSettings, then you can read it here.

Have been worked with it for a long time. Also asked a lot of DotNetNuke gurus, but they did not provide concrete answer about difference between ModuleSettings and TabModuleSettings. In generally it looks the same. For example, we have Settings.ascx. Inside we save some settings, for example Template. So our code should be like this:

Sunday, 8 May 2011

Create User Programmatically in DotNetNuke

One colleague asked about example on how to create user in DotNetNuke programmatically. Here is simple example on C# how to do this:

Saturday, 7 May 2011

Custom Event In The Custom Control

It is good rule to add Custom Events in the Custom Controls you build. It allows easy to use your control from the external code without any problems.

How to add Custom Event? Very easy, just add this code to your class:


// delegate declaration

public delegate void ChangingHandler(object sender);

// event declaration

public event ChangingHandler OnDoFilter;

// this one is to call event

private
void FireOnDoFilter(object sender)
{
  if (OnDoFilter != null)
  {
    OnDoFilter(sender);
  }
}


Custom Events are easy to create and easy to use. Lets use it!

Hope this helps!

Friday, 22 April 2011

Create new page for DotNetNuke in background

Prehistory:
Some customers need to create a lot of pages at one time. This is why "Bulk Pages Creation" has been added to the Pages Admin - Tabs Manager. But few days ago one customer submitted bug. He tried to create about 500 pages in bulk. Less then 100 have been created, but other have been failed. Problem is httpRuntime executionTimeout in the web.config has small time and after 90 seconds (default value) it fails.

History:

Tuesday, 19 April 2011

How to add break to the ModuleActions for DotNetNuke module

Sometimes in the module for DotNetNuke there are a lot of Actions, like this:


It is not good for usability. Would be better to group items somehow with small breaks between Actions in the list, like this:
As you see breaks between items in the menu makes it more user friendly and grouped. it is very easy to add breaks like this, just need to pass special character "~" for Title for ModuleAction object. Here is example of code (C#):


Actions.Add(GetNextActionID(),
"Suppliers",
DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent,
"", "", EditUrl("Currency"), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);


Actions.Add(new DotNetNuke.Entities.Modules.Actions.ModuleAction(GetNextActionID(), "~", ""));


Hope this helps!

Thursday, 31 March 2011

Force file download instead showing in browser

Sometimes we would like to start attachment downloading instead showing in browser, for example for images (like PNG, JPG, GIF, etc). But usually browser displays content instead to prompt for saving. To fix this issue we just need to add one line of the code:

Response.AddHeader("Content-disposition", "attachment; filename=\"photo.png\"");

It adds header to the response and browser will prompt client for file saving.

Hope this helps.

Saturday, 26 March 2011

Recursive references in the projects

This is the most stupid thing i have ever seen in development. Few years ago there was a project from the client. Project was live already and required some major changes for it. Customer sent me a source and once i started to work, i found about 5 recursive references like: project1 references to the project2 and project2 references to the project1. This is very big problem with development, it means something wrong with architecture. I have moved required methods to the separate project and it fixed 4 recursive references. But 1 recursive reference still exists. This issue did not allow to compile project. Fix was easy: need to set in the properties of the reference "Copy Local" = false.

Hope this helps!

Thursday, 24 March 2011

How to send POST in background

Sometimes it needs to send POST in the background and get some info. In our case we had to send POST to the Protx (UK payment gateway - now its SagePay). Here is quick C# script how to do this:


private string ChargeProtx(CreditCardInfo objCardInfo)
{
    System.Net.HttpWebRequest request = 
(System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create("https://ukvpstest.protx.com/VSPSimulator/VSPDirectGateway.asp");


    request.Method = "POST";
    
string postData = "VPSProtocol=2.22&" +
"TxType=PAYMENT&" +
"Vendor=vendorname&" +
"VendorTxCode=" + objCardInfo.OrderID + "&" +
"Amount=" + objCardInfo.Amount.ToString().Replace(",",".")+"&"+
"Currency=GBP&" +
"Description=Direct_payment&" +
"CardHolder=" + objCardInfo.FirstName + " " + objCardInfo.LastName + "&" +
"CardNumber=" + objCardInfo.Number + "&";


    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
    request.ContentLength = byteArray.Length;
    System.IO.Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();


    System.Net.HttpWebResponse responseX = (System.Net.HttpWebResponse) request.GetResponse();
    dataStream = responseX.GetResponseStream();
    System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();


    reader.Close();
    dataStream.Close();
    responseX.Close();


return responseFromServer;
}

Hope this helps!

Wednesday, 23 March 2011

Strip non word or sanitizing text in DotNetNuke

Sometimes in DotNetNuke we would like to prepare some text to the sanitized form. This is very common task for preparing FriendlyUrls. I would like to recommend to use core DNN method:

public static string StripNonWord(string HTML, bool RetainSpace)
Member of DotNetNuke.Common.Utilities.HtmlUtils

It allows to remove not only spaces, but also all non-URL characters.

BTW: This function used in Core DNN on Tabs updating.

Hope this helps!