Friday, August 16, 2013

CSS One Image on Top of Another

This is some simple CSS to overlay one image on top of another.
<div style="position: relative; left: 0; top: 0;">
  <img src="a.jpg" style="position: relative; top: 0; left: 0;"/>
  <img src="b.jpg" style="position: absolute; top: XXX; left: XXX;"/>
a.jpg will be the main image and b.jpg would be the image you want over top.  You can position the overlay image using the top and left attributes.

Thursday, August 8, 2013

C# Class Within Another Class

Sometimes you need a parent class in C# to contain another class, the simple method for doing this is as follows:
class ParentClass
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public ChildClass ChildClass;
class ChildClass
public string MemberNumber { get; set; }
public string Subscription { get; set; }

C# Working With JSON Web Service

string serviceUrl = ConfigurationManager.AppSettings["serviceUrl"].ToString();
var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl );
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.Accept = "application/json";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
string json = "auth_token=" + token; // Not actually JSON format (effectively a simple query string)
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
var result = streamReader.ReadToEnd(); // data in JSON format
// Parse the JSON into custom object
 JavaScriptSerializer serializer1 = new JavaScriptSerializer();
customClass obje = serializer1.Deserialize<customClass>(result); 


In the above example the web service outputs JSON, but takes data in via query string. The code could be adapted to send the data in as JSON as well.

Monday, August 5, 2013

C# DataTable Copy vs Clone

C# DataTable Copy Method
Creates a new copy of the DataTable including structure and data.

C# DataTable Clone Method
Creates a new copy of the DataTable structure, but does not copy the data.

Convert String to XML

A simple way to convert a string to XML is as follows:
string xmlContent = "<ViewFields><FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /></ViewFields>";
XmlDocument nd = new XmlDocument();
XmlNode viewFields = nd.DocumentElement;
In this example we drill down deeper to get an XmlNode (used for querying SP lists).

Hexadecimal value 0x13, is an invalid character

Using the SharePoint 2007 Lists web service, the following error was returned when the GetListItems method was returning XML containing the data.
 hexadecimal value 0x13, is an invalid character. Line XXX, position XXX.
Looking into the error, it appeared a bad character was being returned. I was eventually able to trace the exact record causing the error and it was due to double and triple exclamation marks (which appear to have been copied from Microsoft Word into the text editor).

The request failed with HTTP status 403: Forbidden

When connecting to a SharePoint 2007 lists service the following error message is encountered:
The request failed with HTTP status 403: Forbidden
 The server is using Forms Based Authentication and an auth cookie was generated but never assigned to the service.
newListProxy.CookieContainer = cookieContainer; // Assign Cookie

Working with XmlWriter in C#

The following XML code needed to be generated for the SharePoint 2007 Lists service.
<Batch OnError="Continue" ListVersion="1" 
   <Method ID="1" Cmd="Delete">
      <Field Name='ID'>2</Field>
   <Method ID="2" Cmd="Delete">
      <Field Name='ID'>8</Field>
 It can be done using XmlWriter in C#
using (XmlWriter writer = XmlWriter.Create("test.xml"))
writer.WriteAttributeString("OnError", "Continue");
writer.WriteAttributeString("ListVersion", "1");
writer.WriteAttributeString("ID", "1");
writer.WriteAttributeString("Cmd", "New");
writer.WriteAttributeString("Name", "ID");
writer.WriteString("Item ID");

Thursday, August 1, 2013

Add a Web Reference to a Console Application

For non web projects in c# there is an extra step to add a web reference. Right click on 'References' and then 'Add Service Reference'. A dialog box should appear, at the bottom left hand corner click the 'Advanced' button. Another dialog box will appear, click the 'Add Web Reference' button in the bottom left hand corner. Now you can add your web reference.