Tuesday, August 9, 2016

Using HTML in a Web.config file

I wanted some pop up contextual help in an app I was writing. I got to the point where my requirements were:

  1. Use the bootstrap "popover" so I could include HTML and style the pop-up
  2. Include the help text in the config so it could be easily changed in the future 
Here's how I did it. 

Step 1: add a new section to web.config – this will prevent the text from needing to be escaped or treated with any special handlers

<section name="contextHelp" type="System.Configuration.IgnoreSectionHandler" allowLocation="false" />

Step 2: add the values for the section – I’m using CDATA to hide the markup and that should be sufficient for hyperlinks or images too

    <groupTitle><![CDATA[A group title with specific identifying information will be easy to search for later.<br /><br />Use specific hierarchy to help.


Step 3: add code to read the config in the code-behind

protected string HelpTextTitle { get; set; }

protected new void Page_Init(object sender, EventArgs e)
  // Help Sections
  XmlDocument doc = new XmlDocument();
  HelpTextTitle = doc.SelectSingleNode("/configuration/contextHelp/groupTitle").FirstChild.Value;

Step 4: add values into markup

<a href="#" rel="group_title"><i class="fa fa-question-circle"></i></a>
$(document).ready(function () {
             title : 'Group Title', 
             html: 'true', 
             trigger: 'focus', 
             content: '<%=HelpTextTitle %>'

Friday, July 15, 2016

Authenticating with Web Services written in a different language

Ah, the great promise of web services! We'll all speak the same language, XML/SOAP/JSON! Yeah, not so much. My latest interaction with this was a service that wanted a WS-Security authentication header but didn't want all the fields that Microsoft uses by default; without laying blame my understanding is some Java-based services don't accept a timestamp and this was my experience. Here's how I solved the problem:

WCF wasn’t really an option; because one of the functions had a callback it wanted to do duplex communication which among other things doesn’t support SSL. So here’s what I did:
  1.  Added WSE3 to the project (Microsoft.Web.Services3.dll)

  2. Create an old-school Web Reference in .NET (advanced option in “Add Service Reference”)

  3. Changed the resulting Referencs.cs class from System.Web.Services.Protocols.SoapHttpClientProtocol to Microsoft.Web.Services3.WebServicesClientProtocol

  4. Created a policy to allow me to filter the SOAP

  5. public class ClientUserNamePolicyAssertion : SecurityPolicyAssertion
            public string UserName { get; set; }
            public string Password { get; set; }
            public ClientUserNamePolicyAssertion(string userName, string password)
                this.UserName = userName;
                this.Password = password;

            // and let the assertion know you have an output filter for outgoing SOAP packets
            public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
                return new ClientSendSecurityFilter(this);

            public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
                throw new NotImplementedException();

            public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
                throw new NotImplementedException();

            public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
                return null;

  6. Created the filter – here is where I remove the timestamp header, right before the packet goes out

  7. public class ClientSendSecurityFilter : SendSecurityFilter
            private ClientUserNamePolicyAssertion clientUserNamePolicyAssertion = null;

            public ClientSendSecurityFilter(ClientUserNamePolicyAssertion userNamePolicyAssertion)
                : base(userNamePolicyAssertion.ServiceActor, true)
                this.clientUserNamePolicyAssertion = userNamePolicyAssertion;

            public override void SecureMessage(SoapEnvelope soapEnvelope, Security security)
                UsernameToken usernameToken = new UsernameToken(clientUserNamePolicyAssertion.UserName, clientUserNamePolicyAssertion.Password, PasswordOption.SendPlainText);

            public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
                // go ahead and run the base so the headers and security are added
                SoapFilterResult res = base.ProcessMessage(envelope);

                // remove the timestamp from the security header
                foreach (XmlNode n in envelope.Header.ChildNodes) {
                    if (n.Name == "wsse:Security")
                        foreach (XmlNode p in n)
                            if (p.Name == "wsu:Timestamp") n.RemoveChild(p);


                return res;

  8. Then in the main I can create the service, apply my policy and make calls

TheirJavaService svc = new TheirJavaService();
ClientUserNamePolicyAssertion assert = new ClientUserNamePolicyAssertion("myusername", "mypassword");
Policy policy = new Policy();
TheirServiceRequest req = new TheirServiceRequest();
TheirServiceResponse res = svc.GetResponseFromTheirService(req);

Friday, November 1, 2013

Generating Excel and formatting numbers

I often write HTML tables and pass them off as Excel documents. This is an easy way to automate creation of an Excel sheet and gives you formatting options. The one I can never remember is how to force numbers to be formatted as text cells - to keep long numbers like 143417623220 from showing in scientific notation like 1.43+E11.

So here's the trick. Set a stylesheet and use this to force your numeric cells to format properly.

  mso-number-format:"\@"; /* force text format for numbers */

Thursday, October 18, 2012

Use CAML when searching SharePoint

CAML is worth the effort. I went from this (powershell):

$listitem = $list.Items | Where { ($_.Title -eq $row["Title"]) -and ($_.Name -eq $row["Name"]) }

To this:
$query = New-Object Microsoft.SharePoint.SPQuery 
$caml = '<where><and><eq><fieldref name="Title"><value type="Text">' + $row["Title"] + '</value></fieldref></eq><eq><fieldref name="Name"><value type="Text">' + $row["Name"] + '</value></fieldref></eq></and></where>'
$query.Query = $caml 
$listitem = $list.GetItems($query)[0]
And the code in question is now about 60 times faster. I’m guessing that the first line is scanning every item until it finds the one it wants, while the CAML query somehow avoids that. Big improvement. 

Friday, August 24, 2012

Powershell tip 1: get-member

Get-Member will tell you all the properties and methods of an object.


“this is a string” | Get-Member will return all the string options like split, join, whatever.

Get-SPContentDatabase | Get-Member on a sharepoint server will show you lots of methods you can run on the db, like “DiskSizeRequired” and “Server” to get information about the db.  

So if you have something in powershell and don’t know what to do with it, try piping it to get-member. 

Saturday, August 20, 2011

ASP.NET without Visual Studio

Sometimes you just don't need a project. You're writing a report or something really low rent and you just need to get a page out there quickly. Well you're in luck, inline declarations can be used to obviate the need for a project, references, etc.

You can have these standalone in any IIS web directory or you can make them into an app by putting them in a folder, adding a web.config, declaring an app pool in IIS admin, etc.

You can mix HTML and ASP tags as needed, or generate elements in code (as I've done below).

Your first line needs to declare a page and language used:
<%@ Page Language="VB" Debug="true" %>

You can import libraries from the GAC by using Import
<%@ Import Namespace="System.Text" %>

You can import libraries from outside the GAC using Register Assembly and then Import
<%@ Register Assembly="System.DirectoryServices, Version=2.0.50727.3053, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="System.DirectoryServices" TagPrefix="SD" %>
<%@ Import Namespace="System.DirectoryServices" %>

VB.NET uses a Page_Init function to initialize your web app.
<script runat="server">

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    ' POST takes the usr and pwd fields and authenticates
    If Request.Form.Count > 0 Then
      AuthEntry(Request.Form.Item("usr"), Request.Form.Item("pwd"))
    End If
    ' no parameters will generate a login form
    If Request.QueryString.Count = 0 And Request.Form.Count = 0 Then
    End If
End Sub

' you can generate form elements using code or do more traditional ASP layout below the script tag
Protected Sub GenerateForm()
  Dim Form1 As System.Web.UI.HtmlControls.HtmlForm
  Dim Label1 As System.Web.UI.WebControls.Label
  Dim usr As System.Web.UI.WebControls.TextBox
  Dim pwd As System.Web.UI.WebControls.TextBox
  Dim btn1 As System.Web.UI.WebControls.Button

  Form1 = New HtmlForm()
  Form1.ID = "myForm"

  usr = New TextBox()
  usr.ID = "usr"

  pwd = New TextBox()
  pwd.ID = "pwd"
  pwd.TextMode = TextBoxMode.Password

  btn1 = new Button()
  btn1.Text = "Login"


End Sub

' Here is a function that will validate a user against Active Directory
' You are essentially performing an authenticated search for your own account.
Protected Sub AuthEntry(usr as String, pwd as String)

  Dim path as String = "LDAP://your.domain.com/CN=Users,DC=your,DC=domain,DC=com"
  Dim entry as DirectoryEntry = new DirectoryEntry(path,usr,pwd,AuthenticationTypes.Secure)

    Dim obj as Object = entry.NativeObject
    Dim search as DirectorySearcher = new DirectorySearcher(entry)
    search.Filter = "(SAMAccountName=" + usr + ")"
    Dim result As SearchResultCollection = search.FindAll()
    If result.Count > 0 Then
      ' authenticated
      Exit Sub
    End If
  Catch ex As Exception
    Exit Sub
  End Try

End Sub


Monday, August 30, 2010

Basic sharepoint web part in .NET

1     Created a new Windows Class Library project in VS2008
2     Added a reference to System.Web
3     Added the following ‘using’ statements:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
4     Changed the default class from public class Class1 to public class Hello: WebPart
5     Wrote an override for the “RenderContents” function (I got fancy and stripped off the leading domain portion of the login, and later wrote more code to change the font and add the little % bar)
protected override void RenderContents(HtmlTextWriter writer)
 string name = this.Context.User.Identity.Name;
 writer.Write(" You are logged in as " + name + "! ");
6     Built the dll in release mode and put it in a shared dir on my machine.
7     Logged onto the mossdev server and copied the dll to the sharepoint server into the web server’s ‘bin’ directory (\Inetpub\wwwroot\wss\VirtualDirectories\80\bin)
8     Added a line to web.config to make the server trust the dll (\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config). In the section of web.config, I added a entity that looked like this:
9     Went onto sharepoint, went to site settings (for the whole site), and chose ‘Web Parts’ under Galleries. Clicked New, found my ‘Hello’ part, and clicked ‘Populate Gallery’ which added it for use on the site.

1    Went to a site and added the part to my front page. Opah!