Quicknote – writing a uk date in C#

If you are writing out datetime.now on a machine set to en-gb, you should not need to worry about globalisation if you require uk formatted dates. However if you do need to force the format, you can using the follow ToString overload on DateTime.Now (requires system.globalisation namespace).

DateTime.Now.ToString(new CultureInfo(“en-GB”));

JavaScript: Setting up a scrollable readonly text box

To make a text area readonly through JavaScript whilst allowing the scrollbar to function correctly do the following:

document.getElementById(“theTextArea”).readOnly = true;
document.getElementById(“theTextArea “).style.color = “#777″;
document.getElementById(“theTextArea”).style.backgroundColor = “#EEE”;

Alternatively, you could do document.getElementById(“theTextArea”).disabled = true; but the scrollbar on the text area will not function and you will not be able to see any overflowing text.

SPPS 2003: Setting up advanced search

A quick reminder on how to setup advanced search in SharePoint (just in case I forget – its pretty intuitive to set up, but I do it infrequently).

Enable advanced search

  • Go to portal settings page
  • Click configure search and indexing
  • Click enable advanced search and appropriate confirms.

Add reference to site collection

  • On the portal, click on sites
  • On the actions menu, select “Add Link to site”
  • Specify the title (can be anything) and the url of the site collection e.g. http://myservername/sites/testsite and click ok

Running the search index

  • Go to portal Settings
  • Go to Configure Search And Indexing
  • Go to manage content indexes
  • click drop down for “non_portal_content” and click “start full update”

Add Document properties to the advanced search:

  • Go to portal settings
  • Select Manage Properties from Crawled Documents
  • Expand Urn:schemas-microsoft-com:office:office
  • Click the relevant field(s) you want to use advanced search on and tick “Include this property in Advanced Search Options” and “Display this property in item details in search Results”.
  • After doing so, you will probably want to run the search index again as above.

SharePoint 2003: Impersonating and using administrative functions

I have a case where I need to perform administrative functions (i.e. from the Microsoft.SharePoint.Administration namespace) on a custom page running in SharePoint. The page may be accessed by a user who does not have administrative rights, but the code needs to run anyway. To do this, I ensured that there was no reference to the normal SharePoint digest on the page and instead added a __REQUESTDIGEST hidden field as per the article http://msd2d.com/Content/Tip_viewitem_03NoAuth.aspx?section=Sharepoint&id=15A38A41-F5F1-4FDD-B5D3-6F80E54D988F, but whilst impersonating. I was then subsequently able to user administrative functions when impersonating in later methods. The code shown below references an impersonation class.

Note that I am not sure if this is the correct way to achieve the task, and would be interested to hear any suggestions.

To go in the aspx.cs file


protected override void Page_Load(object sender, System.EventArgs e)
{
    Impersonator imp = new Impersonator();
    imp.Impersonate();
    SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
    Context.Items[SPGlobalAdmin.RequestFromAdminPort] = true;
    Page.RegisterHiddenField("__REQUESTDIGEST", globalAdmin.AdminFormDigest);
    imp.Undo();
}
private void buttonUpdateVersions_Click(object sender, System.EventArgs e)
{
    Impersonator imp = new Impersonator();
    imp.Impersonate();
    // Do something here...
    imp.Undo();
}

SharePoint: Setting meta data properties whilst adding a file to a doc library programmatically

Just a quick note…

I have a versioning document library in SharePoint 2003, and am trying to upload documents to it programmatically. Unfortunately, the only way to set some meta data properties is to update the associated splistitem properties once the file is uploaded. The problem with this is that it generates two entries in the version history (before and after the meta data properties have been set).

I have reflected the SharePoint dll and it looks like there is an overload for adding meta data properties when a file is uploaded that is not accessible. Note that in WSS 3 / SharePoint 2007 a method does seem to be accessible – see http://www.sharepointblogs.com/vandest/archive/2006/11/20/16262.aspx.
Note that when a document is uploaded via the standard SharePoint upload document form, meta data properties can be set without generating two separate versions.

I would be really interested to here if anyone has a solution to this (other than deleting versions).

General: Getting docs to open in application as opposed to browser

To get internet explorer to open documents in the native application instead of the browser window, you must alter a computer setting (not an Internet Explorer setting).

  • Open windows explorer
  • Select tools then folder options from the menu
  • Click the file types tab on the dialog window
  • Select the relevant doctype and click the advanced button
  • Check or uncheck the browse in same window checkbox as appropriate

openingInNativeApp

JavaScript: Dynamically adding an onclick event

The code below shows how to dynamically add an onclick event to a checkbox input element. The most important thing to note is that onclick must be set using a reference to a function (i.e. with no parentheses).

<html>
<head>
<title>JS Test</title>
<script language="javascript">
function addOnClickEvent()
{
    var element = document.getElementById("testcheckbox");
    element.onclick = generateMessage;
}
function generateMessage(message)
{
    alert("Hello World");
}
</script>
</head>
<body onload="addOnClickEvent();">
  <form action="jstest.htm" method="post">
    <input type="checkbox" id="testcheckbox" />
    <input type="submit" name="submit" value="submit" />
  </form>
</body>
</html>