Profil de puneetAchilles WatchesPhotosBlogListesPlus Outils Aide

Blog


JavaScript document.write doesnot work in XML

 
I was working with an XML file which had spit out HTML . This XML file  contained a document.write("<script> .... </script>"); which was breaking the xml file.  For more details go HERE. Anyway, the problem was fixed by writing the javascript in a seperate .js file and it was included in the HTML page.
 
Enjai,
Puneet.H

PKCS7 in .Net and PKCS5Padding in Java with JCE

 
My Client was encrypting using JCE on a app server running Java 1.3. The padding he was using is PKCS5 padding while in .Net i was using PKCS7 padding mode to decrypt. Both of us were using AES/Rijndael algorithm for encryption and decryption purposes.
I was wondering if the two padding modes were compatible. Yes, they are compatible with each other to block sizes between 1 and 255 bytes inclusive.  
For further info go here.
 
Enjai,
Puneet.H

SharePoint - Business Data Catalog walkthrough

 
I was looking out for some BDC related stuff and for some walkthroughs and tutorials.
Here is the MS Virtual Lab link to the walkthrough which i found really helpful.  You have to be a MSDN subscriber and a MSN passport account helps.
 
Also editing the application definition file by hand is a pain. Using BDC MetaMan is advised. You can find it here.
 
Enjai,
Puneet.H

Using SPSecurity.RunWithElevatedPrivileges: Get a File

 
Well, i had a SharePoint site running Forms Authentication and i had to get a file from the document library for editing. So i decided to give Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges method a try. This takes a void function which will be run in elevated priveleges. So creating/deleting MySite for users while adding them from a legacy system can be done without the application running in System Account.
 
Heres the sample code:
 
SPFile rFile;
SPWeb web;
using
(SPSite site = new SPSite(serverurl))
{
web = site.OpenWeb();
// get the file
SPSecurity.RunWithElevatedPrivileges(GetSPFile);
// read the file
StreamReader streamReader =
new StreamReader(rFile.OpenBinaryStream());
filecontent = streamReader.ReadToEnd();
// do some file manipulation
/*
.......
*/
web.AllowUnsafeUpdates =
false;
}
 
private
void GetSPFile()
{
web.AllowUnsafeUpdates =
true;
rFile = web.GetFile(strFileURL);
}
 
Enjai,
Puneet.H

C# + Create a CSV Report

Heres the code to create a CSV Report in C#. Here reader is of type SqlDataReader.

 
// Construct a comma-separated list all the column values in the current row
StringBuilder sb = new StringBuilder(); // to hold csv text file
// Create Header and sheet...
string quoter = @""""""; //
 
for(int j=0;j<reader.FieldCount;j++)
{
sb.Append( reader.GetName(j).ToString() );
// headings
sb.Append(",");
// delimiter
}
sb.Append("\n");
// build the csv contents
string replVal = String.Empty;
while (reader.Read())
{
for(int k=0;k < reader.FieldCount;k++)
{
if(reader.GetValue(k).ToString()==null)
{
sb.Append("\"" + reader.GetValue(k).ToString() +" " + ",");
}
else
{
string temp = reader.GetValue(k).ToString();
replVal = temp.Replace("\"",quoter);
}
replVal+= " " +",";
sb.Append(replVal);
}
//end if
sb.Append("\n");
// new row
}
// end while
reader.Close();
reader =
null;
 
// Write this into the above created file
string strFileContent= sb.ToString();

SharePoint 2007 + Blocked File Types

In order to add or remove a file type being added to your SharePoint portal you will have to add/remove the extension of the file from blocked file types.

To find out the blocked file types you will have to goto Central Administration -> Operations -> Manage Blocked File Types.

You will see a list of extensions. In order to remove a file type, delete the file type and click OK.

In order to block a file type, just add it to the list in a new line and click OK.

For more information go HERE.


SharePoint + HTML Editor

Well, if you wanted a HTML editor in your SharePoint application then read on...
SharePoint uses a HTML editor present in http://Server:portno/_layouts/htmledit.aspx for its own purposes.
 
I have put forth the JavaScript code required to popup the HTML editor. I also have to warn that for some reason you are presented with messy JavaScript errors when you try to highlight the text background or change the color of the text.
 
<script language="javascript">
function jsPopupEditor()
{
var rc = new Array(0,0);
rc = window.showModalDialog('http://Server:portno/_layouts/htmledit.aspx?Target=hdnTxt',document.getElementById("hdnTxt"),'dialogHeight:450px;dialogWidth:450px;');
if (rc != null){
document.getElementById("hdnTxt").innerHTML= rc;
document.getElementById("hdnTxt").value = rc;}
}
</script>
 
Here hdnTxt is a hidden multiline textbox containing the HTML to be edited.
 
Enjai,
-Puneet.H

Convert a HEX string to a string in C#

This piece of code was originally got from Codeproject.com. This class will decode a HEX string to a string.
 
/// <summary>
/// Summary description for HexEncoding.
/// </summary>
public class HexEncoding
{
public HexEncoding()
{
//
// TODO: Add constructor logic here
//
}
public static int GetByteCount(string hexString)
{
int numHexChars = 0;
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
numHexChars++;
}
// if odd number of characters, discard last character
if (numHexChars % 2 != 0)
{
numHexChars--;
}
return numHexChars / 2; // 2 characters per byte
}
/// <summary>
/// Creates a byte array from the hexadecimal string. Each two characters are combined
/// to create one byte. First two hexadecimal characters become first byte in returned array.
/// Non-hexadecimal characters are ignored.
/// </summary>
/// <param name="hexString">string to convert to byte array</param>
/// <param name="discarded">number of characters in string ignored</param>
/// <returns>byte array, in the same left-to-right order as the hexString</returns>
public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
newString += c;
else
discarded++;
}
// if odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length - 1);
}
int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i = 0; i < bytes.Length; i++)
{
hex =
new String(new Char[] { newString[j], newString[j + 1] });
bytes[i] = HexToByte(hex);
j = j + 2;
}
return bytes;
}
public static string ToString(byte[] bytes)
{
string hexString = "";
for (int i = 0; i < bytes.Length; i++)
{
hexString += bytes[i].ToString(
"X2");
}
return hexString;
}
/// <summary>
/// Determines if given string is in proper hexadecimal string format
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static bool InHexFormat(string hexString)
{
bool hexFormat = true;
foreach (char digit in hexString)
{
if (!IsHexDigit(digit))
{
hexFormat =
false;
break;
}
}
return hexFormat;
}
/// <summary>
/// Returns true is c is a hexadecimal digit (A-F, a-f, 0-9)
/// </summary>
/// <param name="c">Character to test</param>
/// <returns>true if hex digit, false if not</returns>
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c =
Char.ToUpper(c);
numChar =
Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
}
/// <summary>
/// Converts 1 or 2 character string into equivalant byte value
/// </summary>
/// <param name="hex">1 or 2 character string</param>
/// <returns>byte</returns>
private static byte HexToByte(string hex)
{
if (hex.Length > 2 || hex.Length <= 0)
throw new ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return newByte;
}
 
}
 
Enjai,
Puneet.H
 

SharePoint + Open documents(links) in a new window

Well to force open the documents(links) in a new window. Get the webpart from here and add it to the document libary page.
Enjai
 
-P.H