Sunday, May 17, 2009

Presenting at Chicago SharePoint Saturday!

I've been selected as a speaker at Chicago SharePoint Saturday!
http://www.sharepointsaturday.org/chicago/meetings/8/200LevelCustomUserProfileImportandSearch.aspx

For all of those interested, here is the link to the event:
http://www.sharepointsaturday.org/chicago/default.aspx

I'll be doing a presentation about custom active directory Import into the SharePoint user profile. 
Wish me luck!

Thursday, March 26, 2009

Removing a workflow from deleted items

A client of mine has a SharePoint task list that has a SharePoint Designer workflow attached to it. The workflow was a basic notification workflow. When an item is added to the list, the workflow kicks off and sends reminder emails to the assigned person 30, 15, 10,7,3 and 1 days before the task is due if it’s not completed.

 

It’s a straight forward workflow modeled off of the Notification workflow that comes with the Employee Training site template provided by Microsoft.

 

The Problem:

When items are deleted from the list, the workflows are not cancelled. People were still getting emails for items that were deleted!

An easy fix for this is to attach a simple event handler to the list and on the “OnDeleting” event, remove all workflows from the item. This will works for all items moving forward, but it doesn’t stop the workflows that have already been started. In this case there were many items that had been deleted with running workflows.

 

What I wanted to do was to go through the recycle bin and delete all of the workflows from all items that came from that list.  I figured this would be easy enough, but it turns out I was mistaken. There is no way to get an instance of a workflow to kill it without having the ID of the item it was attached to, and you can’t get the ID of an item that resides in the recycling bin.

 

Solution:

Let me start by saying I think that this solution is “hackish” at best.

Here are the steps I took

1.       Using an SPQuery object, Take a snap shot of the current list (Before)

2.       Restore all of the items out of the recycle bin that came from the list

3.       Using an SPQuery object, take another snap shot of the list after restoring all items (After)

4.       Check each item in the “after” list and see if it resides in the “before” list

5.       If it does not exist

a.       The item was a restored item

b.      Get all workflows for this restored item and delete all associated workflows

c.       Send the item back to the recycle bin

 

That’s it.

The code is posted below. 

Hope this helps someone else!

static void Main(string[] args)

        {

            WriteToLogFile(" ****** Starting on:" + DateTime.Now.ToString() + " ****** ");

            try

            {

                using (SPSite site = new SPSite(ConfigurationManager.AppSettings["SITE_URL"]))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        WriteToLogFile("Opened Web:" + web.Url);

                        SPList list = web.Lists[ConfigurationManager.AppSettings["LIST_NAME"]];

                        string url = list.DefaultViewUrl.Substring(0, list.DefaultViewUrl.LastIndexOf("/"));

                        url = url.Substring(1);

                        WriteToLogFile("Got List:" + url);

                        

                        SPListItemCollection before = GetListItems(list);

                        WriteToLogFile("Count before:" + before.Count.ToString());

                        before.GetDataTable().WriteXml("Before.xml");

                        

                        SPRecycleBinItemCollection bin = web.RecycleBin;


                        WriteToLogFile("Url:" + url);


                        for (int i = bin.Count - 1; i >= 0; i--)

                        {

                            WriteToLogFile("DirName:" + bin[i].DirName);

                            if (url == bin[i].DirName)

                            {

                                WriteToLogFile("Restoring:" + bin[i].Title);

                                bin[i].Restore();

                            }

                            else

                            {

                                WriteToLogFile("Not restoring:" + bin[i].Title);

                            }

                        }


                        SPListItemCollection after = GetListItems(list);

                        

                        WriteToLogFile("Count after:" + after.Count.ToString());

                        after.GetDataTable().WriteXml("After.xml");


                        if (before.Count == after.Count)

                        {

                            WriteToLogFile("Before and After counts match. No work to be done!");

                            return;

                        }


                        WriteToLogFile("Before and After do not match. Digging through after list to see which items have been restored");


                        for (int i = after.Count-1; i >= 0; i--)

                        {

                            SPListItem itemBefore = null;


                            try

                            {

                                itemBefore = before.GetItemById(after[i].ID);

                                WriteToLogFile("Item found. Not Deleting item ID:" + itemBefore.ID.ToString() + " - " +  itemBefore.Title);

                            }

                            catch

                            {}


                            //Item Not Found before, remove workflows and delete it.

                            if (itemBefore == null)

                            {

                                WriteToLogFile("Restored item found. ID:" + after[i].ID.ToString() + " - " + after[i].Title);

                                RemoveWorkflowsFromItem(after[i]);

                                WriteToLogFile("Sending Item ID:"+after[i].ID.ToString()+" to Recycle bin");

                                after[i].Recycle();

                            }

                        }


                    }

                }

            }

            catch (Exception ex)

            {

                WriteToLogFile(ex.ToString());

            }

            

            WriteToLogFile(" ****** Complete on:" + DateTime.Now.ToString() + " ****** ");

            

        }


        private static void RemoveWorkflowsFromItem(SPListItem item)

        {

            SPWorkflowManager mgr = item.ParentList.ParentWeb.Site.WorkflowManager;


            SPWorkflowCollection coll = mgr.GetItemWorkflows(item);


            if (coll.Count > 0)

            {

                WriteToLogFile("Removing " + coll.Count.ToString() + " workflows for:" + item.Title);


                for (int w = coll.Count - 1; w >= 0; w--)

                {

                    mgr.RemoveWorkflowFromListItem(coll[w]);

                }

            }

            else

            {

                WriteToLogFile("No workflows to remove from " + item.Title);

            }


        }



        private static SPListItemCollection GetListItems(SPList tasks)

        {

            SPQuery query = new SPQuery();

            query.Query = "0";

            return tasks.GetItems(query);

        }


        private static void WriteToLogFile(string message)

        {

            System.IO.StreamWriter sw = new System.IO.StreamWriter("Workflowcleaner.txt",true);

            sw.WriteLine(message);

            sw.Close();

        }

    }

 

Monday, February 2, 2009

SPDisposeCheck Released!

I've seen posts about this in the past and its finally released. I'm going to start using this on the code I develop as this can be a tricky thing to master, especially at first.


Wednesday, January 28, 2009

STSADM - "Command line error"

I got this question from a client and its one I've wrestled with before. The solution is quite embarrassing, but not always easy to uncover.

The issue in question was this: 

 The SharePoint admin was trying to do a backup of the current SharePoint site. I had left instructions on how to do this before I left so he just copied/pasted my instructions into the command line. After hitting “enter “he got the lovely "Command Line Error" message. He said he spend the whole morning trying to figure out what the issue was.

What was the Solution?

Microsoft word had modified the dashes in the command to the double dash thing. I told him to simply type it all out instead of pasting in the command.

Success!!

Embarrassing no?

So if you are having a command line error and you are pasting in the command, try to type out the command by hand. 

Enjoy!

Wednesday, January 14, 2009

Exposing InfoPath Forms via URL

At my current client, we published various InfoPath forms to Forms Libraries. We needed a way to expose the forms as links throughout the site. 

At first I figured this would be easy, but it turns out it is not.

I tried this first:
http://[server]/[site]/[list]/Forms/Template.xsn.

Perfect! Except that this does not work in FireFox. Turns out that even with a link directly to the file, SharePoint runs some javascript behind the scenes to see how the file should be displayed. It attempts to create an ActiveX Object, which fails in Firefox. The function is written poorly as when the ActiveX create fails, it attempts to open the form using Form Services. 
Our form was not Form Services compatible so we were getting errors when Form Services tried to expose the form.

Long story short, it didn't work.

I began digging and found other posts that simply paste the JavaScript that SharePoint uses into a Content editor web part. Unfortunately that wasn't going to work for me as our client utilizes links lists and needed a way to expose them via a URL.

That didn't work either.

After digging around in the javascript files a little more I decided to take a new route. 
I installed and monitored the network traffic with Fiddler. I found that SharePoint actually changes pages a few times before the forms are exposed. I tried the different links it was navigating to until one worked.

Here is the solution!

https://[server]/[site]/[list]/forms/template.xsn?OpenIn=PreferClient&NoRedirect=true&XsnLocation=/[site]/[List]/forms/template.xsn

Hope this saves someone else some headache!

Monday, January 12, 2009

CQWP and RSS Feeds

I decided to do a SharePoint blog, so here goes!

At my current client, we have a well defined Content Type structure. On the home page of their intranet they have CQWP's that roll up announcements, alerts etc from all over the site.

They needed a way for users to subscribe to the alerts. Since they were rolling up the content, having the users go to each list and subscribe to alerts was not going to work.

Since we were using CQWP's I decided to enable the RSS feed functionality of the CQWP. In the presentation section of the web part I checked the "Enable feed for this web part" and gave it a name.




I saved the web part and now a new shiny RSS icon appeared at the bottom of the web part:




Easy enough right? Not so fast.



I clicked on the RSS feed icon and got an ugly SharePoint error.


"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). at System.Guid..ctor(String g) at Microsoft.SharePoint.Publishing.Internal.CodeBehind.FeedPage.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) "


I did some research into this and I saw a few people with the error, but no one with any cut and dry solution. I continued to try a few things and came across the solution (for me at least)



In my current implementation, we have the publishing Infrastructure Site collection features turned on, but not the Office SharePoint Server Publishing features at the site level. Therefore my CQWP was on the default page (http://server/default.aspx). By adding a pages library called "Pages", adding a page to it and dropping my CQWP onto that page, the RSS feed works!



Turns out the CQWP is assuming it's situated on a web part page in a "Pages" page library.


So as a workaround, created a small Content Editor web part that sits below the CQWP on the default page, and linked to the RSS feed page of the identical CQWP that resides on the Web Part page that is in the pages library...





Works like a charm!


Enjoy!!
Hopefully there will be many more to come!