Checking for null or blank value in case of post update plugin using ForceSubmit


We were supposed to write a plugin against say entity A which would be updating a related entity B  only if the value of one of the attribute(string)  in entity A is blank.

The plugin was a post update one , so even though we had registered pre/post image with that particular attribute and as it was null, the property/value wasn’t getting passed.

So there was no way to access it in the entity images.

Only way to access the value was to make use of query expression class to get the value saved in the table for that entity. But we decided to use another approach.

Normally if we set a ForceSubmit on a particular field it is passed as one of the property of inputparameters of the context.

So what we did was set ForceSubmit on the field and then converted the inputparameters properties as Dynamic Entity and were able to get the value of that attribute even in case when it was null/blank.

DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[“Target”];

if (entity.Properties.Contains(“new_attSchemaName”))
{
String   myAttrValue = (string)entity.Properties[“new_attSchemaName”];
if (myAttrValue == “”)
{

//

Bye..

Using Encryption and Decryption in an ASP page


Hi I was looking for implementing cryptography in an ASP page.

I found this following article of great use

http://www.4guysfromrolla.com/webtech/010100-1.shtml

The inc file

<%
Dim sbox(255)
Dim key(255)

Sub RC4Initialize(strPwd)
dim tempSwap
dim a
dim b
intLength = len(strPwd)
For a = 0 To 255
key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
sbox(a) = a
next

b = 0
For a = 0 To 255

b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next
End Sub

Function EnDeCrypt(plaintxt, psw)

dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher

i = 0
j = 0

RC4Initialize psw
For a = 1 To Len(plaintxt)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)

cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next
EnDeCrypt = cipher
End Function

%>

And following is the code for my asp page

<!–#include file=”g.inc”–>

<html>
<head>
<title>TestPage</title>
</head>
<body>
<form action=”G.asp” method=”post”>
<%
dim psw, txt
dim strTemp
txt=”NishantRana”
psw=”myPassword”
strTemp=EnDeCrypt(txt,psw)
response.write “Encrypted Text =” & strTemp & “</Br>”
txt=EnDeCrypt(strTemp,psw)
response.write “Decrypted Text =” & txt
%>
</form>

</body>

</html>

Just copy and paste the code

That’s it …


IIS not serving ASP.NET pages


It mostly happens when IIS is installed after .NET is installed. So in this case we need to make use of aspnet_regiis.exe utility.
1) Go to command prompt
2) Go to the following path
“C:WINDOWSMicrosoft.NETFrameworkv2.0.50727”
depending on the version.
3) Enter “aspnet_regiis.exe -i”
4) It will display the message finished installing
5) Open your IIS
6) Select web service extensions
7) Click on Allow for ASP.NET v2.0.50727

That’s it . Now the IIS has been configured properly

Changing authentication mode for Sql Server express edition


One way of doing it is through sql server management studio explained over here

http://msdn.microsoft.com/en-us/library/ms188670.aspx

But if you haven’t installed the management studio than in that case you need to make following change in the registry

HKEY_LOCAL_MACHINE / SOFTWARE / Microsoft / Microsoft SQL server / MSSQL.1/ MSSQLSERVER/ LoginMode

Click on it change it’s value from 1 to 2.

Then Restart your sql server service !!!

After you have done this you need to enable you sa account for sql server authentication for this follow these steps

1) Open SQL Server Management Studio Express and Login
2) In Object Explorer, click Security followed by Logins
3) Right click sa user and select Properties.
4) Uncheck Enforce password policy
5) Select Status. Click the Grant and Enabled radio buttons. Click OK
6) Restart your database server and select restart.

That’s it

Writing a simple Event Reciever or Event Handler in Sharepoint


What is Event handling ?

The ability to catch certain user actions in code and respond programmmatically. These are the classes which we can inherit and write event handlers or receivers against them.

SPItemEventReceiver

SPListEventReceiver

SPWebEventReceiver

Which user actions you can catch?

ItemFileMove,IFMoving, ItemUpdated,ItemUpdating, ItemCheckedIn/Out, ItemCheckingIn/ItemChecingOut, ItemAdded,IAdding, ItemAttachementAdded/Adding, ItemDeleted/Deleting.

What is template id for customlist, document library, survey?

100 Custom List 101 Document Library 102 Survey 103 Links List 104 Announcements 105 Contacts

106 Events List 107 Tasks List 108 Discussion Board 109 Picture Library

Explain the steps for creating event handlers?

1) Create a class library project.

2) Add reference to Microsoft.Sharepoint.dll

3) Inherit the class SPItemEventReceiver

4) Override the method against which you want to attach the event handler.

e.g. for itemdeleting

namespace MyEventHandler{

public class ItemDeletingHandler : SPItemEventReceiver{

public override void ItemDeleting(SPItemEventProperties properties){

// We want to attach the eventhandler against a document library

// named MyDocumentLibrary

if(properties.ListTitle == “MyDocumentLibrary”){

// only the admin user has right to delete the uploaded document

if (!properties.UserLoginName.Contains(“domainnameadminUserName”)){

properties.ErrorMessage = “You don’t have sufficient privelege to delete on list “+properties.ListTitle;

properties.Cancel = true;}}}}}

5) Sign the assembly and put it in GAC i.e. c:windowsassembly

6) Create a new folder with the same name as your eventhandler at the following path

c:program filescommon filesmicrosoft sharedweb server extension12templateFeatures folder

7) Create two files feature.xml and elements.xml at the newly created folder.

8) Take feature.xml file of announcement and make the necessary changes to it.

<?xml version=1.0 encoding=utf-8?>

<Feature Id=C50FF499-B52A-471f-A9FB-36A49E2FA49F

Title=MyEventHandler

Description=For restricting delete of a list

Scope=Web

xmlns=http://schemas.microsoft.com/sharepoint/>

<ElementManifests>

<ElementManifest Location=Elements.xml/>

</ElementManifests>

</Feature>

9) Within elements.xml specify

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

<Receivers ListTemplateId=101>

<Receiver>

<Name>MyEventHandler</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>20000</SequenceNumber>

<Assembly>

MyEventHandler, Version=1.0.0.0,

culture=neutral, PublicKeyToken=e185cb2eba5d0774

</Assembly>

<Class>MyEventHandler.ItemDeletingHandler</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>

Get the values for assembly tag by right clicking the assembly and selecting properties within the GAC.

If we have multiple eventhandler attached to a same event we can specify which handler should be executed first through SequenceNumber tag. It’s value should start from 20000

10) Now install the feature

stsadm.exe -o installfeature -filename MyEventHandlerfeature\Feature.xml

11) Activate the feature

stsadm -o activatefeature -filename MyEventHandlerfeature\Feature.xml -URL http://localhost.

That’s it…

Creating a simple web part with controls in SharePoint


In case when we need to add controls to our web part we need to override the CreateChildControls method of the base WebPart class along with Render method.

Let’s take a simple example for it,  a kind of sample calculator which shows sum of two numbers ]

For this we have added 2 labels, 3 textboxes and 1 button.

On Click of the button the sum of the values entered in textbox1 and textbox2 would be displayed in the textbox3.

SampleCalculatorWebPart
SampleCalculatorWebPart

Following is the code for the above web part.

namespace SampleCalculator

{

public class MyCalculator :WebPart

{

protected Label lblFirstValue;

protected Label lblSecondValue;

protected TextBox txtFirstValue;

protected TextBox txtSecondValue;

protected Button btnCalculate;

protected TextBox txtTotal;

// controls would be initialized and added here

protected override void CreateChildControls(){

lblFirstValue = new Label();

lblSecondValue = new Label();

txtFirstValue = new TextBox();

txtSecondValue = new TextBox();

btnCalculate = new Button();

txtTotal = new TextBox();

lblFirstValue.Text = “Enter First Value :”;

lblSecondValue.Text = “Enter Second Value :”;

btnCalculate.Text = “Calculate”;

// adding eventhandler for click of btnCalculate

btnCalculate.Click += new EventHandler(btnCalculate_Click);

this.Controls.Add(lblFirstValue);

this.Controls.Add(txtFirstValue);

this.Controls.Add(lblSecondValue);

this.Controls.Add(txtSecondValue);

this.Controls.Add(btnCalculate);

this.Controls.Add(txtTotal);

}

void btnCalculate_Click(object sender, EventArgs e){

int total = CalculateSum( Convert.ToInt32(txtFirstValue.Text), Convert.ToInt32(txtSecondValue.Text));

txtTotal.Text= total.ToString();

}

protected override void Render(HtmlTextWriter writer){

// for proper alignment of the controls added

// we can create a table

// <Table>

//<tr><td><td></tr>

//<tr><td><td></tr>

//<tr><td><td></tr>

//</Table>

writer.RenderBeginTag(HtmlTextWriterTag.Table);

writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

lblFirstValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtFirstValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr


writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

lblSecondValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtSecondValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr


writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

btnCalculate.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtTotal.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr

writer.RenderEndTag();// table

}

private int CalculateSum(int a, int b){

return a + b;}

}

}

Put the following attribute in your assemblyinfo.cs file

[assembly: AllowPartiallyTrustedCallers]


Strong sign the assembly and install it in GAC.

Open the web.config of your site where you want this webpart

Make a safecontrol entry within the web.config for your webpart.

<SafeControl Assembly=SampleCalculator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c6ce78ef8448ffa Namespace=SampleCalculator TypeName=* Safe=True />

The name of the assembly,it’s version, culture and public key token information can be found by right clicking the assembly within gac and selecting properties.

Bye..

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓