Wednesday, October 22, 2008

FIPS and SharePoint

Hello SharePointers,


Well, one monkey off the back at long last. Way back in February 2008 we ran into a problem where SharePoint would not work when the "System Crptography: Use FIPS compliant algorithms for encryption, hashing and signing" AD security policy was set to Enabled. SharePoint would come up with "An unexpected error occurred", with some 6482 messages in the Event Logs.


Well, along came the August Cumulative Updates, which apparently resolved the issue. So I installed the CU's per the instructions here, enabled FIPS, and what do you know, it still didn't work, still got "An unexpected error occurred"!!!


Well, turns out that the fix for SharePoint indeed just disables logging of those 6482 error codes. In addition to those CU's, you still need to update the web.config for the SharePoint site, so that it tells .Net to use the encryption that FIPS requires. Which oddly enough doesn't seem to be as strong as the non-FIPS encryption, but then I'm no expert! Please see this article for the details on how to modify the machinekey setting to use the 3DES algorith. Good luck!!

Tuesday, August 19, 2008

Custom ServerURL field in a SharePoint list

Well here's an odd one. I'm using a SharePoint list to store configuration info for a web service I'm using. I wanted the URL of the server to be a parameter, so I could switch between dev, model office & production easily. So logically enough I created a custom field called ServerURL to point to the server hosting the web service. Got some weird results:

Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

Turns out ServerURL is an internal field - when I reference ServerURL0 then I get my own field. Reckon it's time to re-create that field with a different name!

Tuesday, April 29, 2008

Day of Dot Net - West Michigan

Woo hoo, accepted as a speaker at the 2008 Day of Dot Net - West Michigan! Good sessions...I want to see the one about using the Wii remote, that'll be very cool. I'll be talking about the fun I've been having with SharePoint, VS 2008, and Workflows. Good stuff!

WM Day of .Net May 10, 2008 - I'll be there!

Showing icons in a list view

My customer threw me a challenge: instead of your typical Red/Amber/Green SLA indicator, they also want to show Blue if they've exceeded the SLA target. And no yellow - either you're red, missed; green, on; or blue, exceeded. So much for using the KPI interface!

I started looking in to the KPI Dashboard sample to see if I could somehow customize this, but after an hour and a cup of Starbuck's it was still none too clear. Then the coffee cleared away the neurons & I recalled seeing discussion on using the spankin' new XSLT Data Viewer. I had done used this with SPS 2003 and it was pretty good then...and it's better now with MOSS.

The XSLT Data Viewer is a MOSS goodie, sorry WSS'ers. To use it, I fired up SharePoint Designer, opened up my list, changed to the 'Split' view, then with a right click I chose the Convert to XSLT Data View option.

In my list I have a SLA column, a Target column, and a Status column. I wanted the indicator in the Status column.

After a few seconds (slow on the VPC for some reason) I was able to view the list. Looking at the XSLT, for the Status column, I changed what was there to:

<!--Status-->
<TD Class="{$IDAVVHVE}"><xsl:choose>
<xsl:when test="@SLA amp-gt; @Target">
<img src="/_layouts/images/KPIDefault-Blue.gif" alt="Exceeded SLA Target"/>
</xsl:when>
<xsl:when test="@SLA= @Target">
<img src="/_layouts/images/KPIDefault-2.gif" alt="Met SLA"/>
</xsl:when>
<xsl:when test="@SLA amp-lt; @Target">
<img src="/_layouts/images/KPIDefault-0.gif" alt="Missed SLA"/>
</xsl:when>
</xsl:choose>
</TD>

Change the 'amp-gt;' to an ampersandgt; (I should find a better blog page) and 'amp-lt;' to the ampersandlt; and you're good to go!

Now I need to come up with a nice shape for the Blue icon. Blue Moon? mmmm, tasty, good idea!

Tuesday, April 08, 2008

EditControlBlock registrationtypes

Been working on adding a new item to the SharePoint drop down menus...easy to do by creating a feature, then defining the custom action one wants to add in. But...I only want my new menu item available for documents. One of my testers saw that the new menu item was on the folder drop down list also, and what do you know, test case failed!

So...I could either add code that points out to the dear user that one ought not use this with folders, or see if there was a way to hide the new menu item from folders - that is, the new menu item appears only for documents.

RegistrationType and RegistrationID to the rescue. These items are part of the custom action XML defined by the Feature.XML. Here's what I've been able to determine. RegistrationType allows for "ContentType" and "FileType" values, as well as List and ProgID.

Specifying "ContentType" is what I'm after. This will allow me to associate the new action with a specific content type. The RegistrationID element is used to specify the ID of the content type. To get this ID, open your document library settings, in the ContentType section click on the content type of interest, then grab the hex string in the URL after the "ctype=" parameter. Paste this value in to the RegistrationID value.

Alternatively, if I only want this feature to appear for ".xls" files, then I'd use the FileType RegistationType, and the value "xls" for the registationID. Repeat the CustomAction block for other file types.

The completed Feature.XML looks like:

Feature.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="1a8e5fae-577f-4cbd-addb-a31d875300a6"
Title="Copy Document"
Description="My custom document copy feature"
Version="1.0.0.0"
Scope="Web"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="CopyDocument.xml" />
</ElementManifests>
</Feature>


CopyDocument.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Per Item Dropdown (ECB)-->
<CustomAction
Id="Custom.PromoteDocument"
RegistrationType="ContentType"
RegistrationId="ctype=0x01010038C4691B76BEB34CB53E65CADDF82AD3"
Location="EditControlBlock"
Rights="EditListItems"
Title="Promote Document">
<UrlAction Url="~site/_layouts/MyCustomCopyDocument.aspx?List={ListId}&ID={ItemId}"/>
</CustomAction>
</Elements>

Note also the use of {ListId} and {ItemID} - I use this with my code behind file to get the SPList and SPListItem from the content SPWeb. With these I can then get to the attachment object of the SPListItem, so I can copy that document around.

Monday, January 28, 2008

Change AD password with code

New cool stuff with the .Net 3.5 Framework! The System.DirectoryServices.AccountManagement namespace adds all kinds of clean methods for working with user objects. I wanted to put together a simple web part to change AD passwords - found a few good examples out there, but since I have the 3.5 Framework installed on my server I of course had to give the latest a whirl.

First, for background, take a look at this excellent article: AccountManagement Namespace

Joe & Ethan give a great overview of using the new features in 3.5.

In my web part, then, I have a three text boxes and a "Change" button - the bulk of the web part is based on the work done by Robin Meuré did here, with a few cosmetic changes but the main change done to the code in the btn_Click, posted here:

SPWeb webContext = SPControl.GetContextWeb(Context);
string strLoginName = webContext.CurrentUser.LoginName;
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain);

UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, strLoginName);

try
{
string oldPasswordValue = oldpassword.Text;
string newPasswordValue = newpassword.Text;
user.ChangePassword(oldPasswordValue, newPasswordValue);
output.Text = "Password changed. Please close this browser window and log back on with your new password.";
}
catch (Exception ex)
{
output.Text += String.Format("Password couldn't be changed due to restrictions: {0}", ex.Message);

}
finally
{
user.Dispose();
domainContext.Dispose();
}


That's it! Easy-peasy.

Note that when I was getting the user object, I first tried just getting a UserPrincipal object by context:

UserPrincipal user = new UserPrincipal(domainContext);

Problem here was, when i invoked the ChangePassword method, I got the very friendly message back that: ChangePassword method can not be called on an unpersisted Principal object. So instead I get the user account info from the SPContext object, pass that in to the Directory services methods, and we're good to go.

Thursday, January 17, 2008

To copy a file from one lib to another...

Finally back to SharePoint coding after a long hiatus of process documentation - feels great!

So the challenge has been adding a menu item to the Edit Control Block to copy items from one library to another. There are some good examples out there, Andrew Connell's blog has a great example, so I won't repeat it - but I will show all of the code I have. Might help you out.

In this scenario, I have three "document center" on my portal: Working, Staging, and Production ("Docs"). I want to let my users choose when to promote a document, but I didn't want this to be workflow dependent: ie, not based on setting a document property, or based on a check in event, etc - let the user decide.

The drop down for the Edit Control Block was the answer. "Promote Document" will now appear for users with the right access. This feature passes the list ID and item ID to an ASPX page, which then does the work of copying the document over.

Some helpful hints:

In your feature's XML file, you can assign the "Rights" value to the minimum rights a user needs in order to see your new ECB item. That way it won't show up for everyone - nice!

Inside the code, after moving the file, the field items won't copy also. So you'll need to copy them with code. But then when you perform a ListItem.Update the ModifiedBy turns in to the System Account (assuming impersonation) - ick! So intead use the ever cool SPListItem.SystemUpdate() which does *not* change the Modified or ModifiedBy properties.

Here we are:

Feature.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="1a8e5f42-5742-4c42-ad42-a31d87530042" // enter your own guid
Title="Promote Document"
Description="Promote a document from work to staging, or from staging to production" Version="1.0.0.0"
Scope="Web"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="PromoteDocument.xml" /> <
/ElementManifests>
</Feature>

The CopytoWorking.XML file, the manifest listed in feature.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Per Item Dropdown (ECB)-->
<CustomAction
Id="CopytoWorking"
RegistrationType="ContentType"
RegistrationId="0x01"
Location="EditControlBlock"
Rights="EditListItems"
Title="Promote Document">
<UrlAction Url="~site/_layouts/PromoteDocument.aspx?List={ListId}&ID={ItemId}"/> </CustomAction>
</Elements>

and finally the ASPX page. This uses the Application.Master page layout so the new page looks sort of like a SharePoint page. Worked for me!
<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Page language="C#" MasterPageFile="~/_layouts/application.master" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="~/_controltemplates/ButtonSection.ascx" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Net" %>
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
<asp:Literal id="PageTitleLabel1" runat="server" ></asp:Literal>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
<asp:Label id="PageTitleLabel" runat="server" ></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script runat="server">
public void CancelClick(Object obj, EventArgs e)
{
SPWeb oWeb = SPContext.Current.Web;
SPList oList = oWeb.Lists[new Guid(Context.Request["List"])];
Response.Redirect(oList.DefaultViewUrl);
}

public void ConfirmClick(Object obj, EventArgs e)
{
SPWeb oWeb = SPContext.Current.Web;
SPUser currentUser = oWeb.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
oWeb.AllowUnsafeUpdates = true;
SPList oList = oWeb.Lists[new Guid(Context.Request["List"])];
SPListItem oItem = oList.GetItemById(int.Parse(Context.Request["ID"]));
byte[] fileContent = oItem.File.OpenBinary();
String currentUrl = oItem.File.ServerRelativeUrl;
string[] currentUrlSplit = currentUrl.Split('/');
if (currentUrlSplit[1].CompareTo("Staging") == 0)
{ currentUrlSplit[1] = "Docs"; }
if (currentUrlSplit[1].CompareTo("Working") == 0)
{ currentUrlSplit[1] = "Staging"; }

StringBuilder sb = new StringBuilder();
String siteUrl = oWeb.ParentWeb.Url;
string[] siteUrlSplit = siteUrl.Split('/');
sb.Append(siteUrlSplit[0]); // this is the protocol
sb.Append("//");
sb.Append(siteUrlSplit[2]); // this is the server
string server = sb.ToString();
// now add the rest of the URL - site & folders
for (int i = 1; i < currentUrlSplit.Length; i++)
{
sb.Append("/");
sb.Append(currentUrlSplit[i]);
}
String newUrl = sb.ToString();
SPWeb newWeb = new SPSite(newUrl).OpenWeb();
newWeb.AllowUnsafeUpdates = true;
EnsureParentFolder(newWeb, newUrl);

// copy the file over
SPFile newFile = newWeb.Files.Add(newUrl, fileContent, currentUser, currentUser, oItem.File.TimeCreated, oItem.File.TimeLastModified);

// add the properties
foreach (SPField oField in oItem.Fields)
{
if (!oField.ReadOnlyField &&
oField.InternalName != "ContentType" &&
oField.Type != SPFieldType.Invalid &&
oField.Type != SPFieldType.WorkflowStatus &&
oField.Type != SPFieldType.File &&
oField.Type != SPFieldType.Computed &&
oField.Type != SPFieldType.User &&
oField.Type != SPFieldType.Lookup)
{
newFile.Item[oField.Title] = oItem[oField.Title];
}
}

newFile.Item.SystemUpdate(true);
newWeb.Dispose();
Response.Redirect(oList.DefaultViewUrl);
});
}

public static string EnsureParentFolder(SPWeb parentSite, string destinUrl)
{
destinUrl = parentSite.GetFile(destinUrl).Url;
int index = destinUrl.LastIndexOf("/");
string parentFolderUrl = string.Empty;
if (index > -1)
{
parentFolderUrl = destinUrl.Substring(0, index);
SPFolder parentFolder = parentSite.GetFolder(parentFolderUrl);
if (!parentFolder.Exists)
{
SPFolder currentFolder = parentSite.RootFolder;
foreach (string folder in parentFolderUrl.Split('/'))
{
currentFolder = currentFolder.SubFolders.Add(folder);
}
}
}

return parentFolderUrl;
}
</script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderId="PlaceHolderPageImage" runat="server"> <img src="/_layouts/images/blank.gif" width=1 height=1 alt="" /></asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">
<asp:Label id="PageDescriptionLabel" runat="server" ></asp:Label>
<asp:PlaceHolder id="PublishDescription" Visible="true" runat="server">
<tr> <TD height="8px"><img src="/_layouts/images/blank.gif" width="1" height="8" alt=""></td></tr>
<tr><td colspan="2">
<table cellpadding="2" cellspacing="1" width="100%" class="ms-informationbar" style="margin-bottom: 5px;" border=0>
<tr><td width="10" valign="middle" style="padding: 4px">
<img id="Img1" src="/_layouts/images/exclaim.gif" alt="<%$Resources:wss,exclaim_icon%>" runat="server"/></td>
<td class="ms-descriptiontext"><SharePoint:EncodedLiteral ID="EncodedLiteral1" runat="server" text="Click Confirm to continue or Cancel to return. Promoting a document will remove it from this library and move it from Work to Staging, or Staging to Production." EncodeMethod='HtmlEncode'/>
</td></tr></table>
</td></tr>
</asp:PlaceHolder></asp:Content>
<asp:Content ID="Content6" ContentPlaceholderID="PlaceHolderMain" runat="server">
<asp:Button CssClass="ms-ButtonHeightWidth" AccessKey="o" ID="Confirm" runat="server" Text="Confirm" OnClick="ConfirmClick"/>
<asp:Button CssClass="ms-ButtonHeightWidth" AccessKey="a" ID="Cancel" runat="server" Text = "Cancel" OnClick = "CancelClick" />
</asp:Content>