Using .Net and the CLR means we have sometimes have to use both VB and C#. If you are anything like me you will forget the more subtle syntactical differences between the two more often than not, a situation I found myself in earlier today when trying to work out how to rewrite an old VB function in C#.
Rather than doing this myself (I very rarely use VB anymore) I decided to try to use some of the resources on the internet. The following sites will all allow you to convert freely between the two languages by pasting code into a browser control:
CodeTranslator: Code Translation From VB.NET <-> C# - CarlosAG-> - my favourite - this one managed to convert things the others couldn't.
Convert VB.NET to C# - developerFusion
Convert VB to C# or C# to VB provided by Telerik
Hope this helps!
Tuesday, 17 September 2013
Saturday, 22 June 2013
Changing the name and URL of a document in Sharepoint using c#
Changing a document's name in Sharepoint programatically, particularly in a webpart for example, is not a trivial process. In theory, we can use the SPListItem.Item("Name") property and update this. However you may find that non-administrative users can receive "permission denied" error when they save this back to the database (using a web.Update()).
This is generally because in order to move (rename) a file in Sharepoint, the user must have "delete" permission on list.
Provided the users have enough permission, then the code below works. The trick here is to use an SPFile object to update the "Name" property, whilst using an SPListItem object to control the files checkin/out status.
Keen readers will see that I am doing a ValidateFormDigest() in order to prevent the "Page Validation" error if this is caused from a form's POST action, but there is also a "web.AllowUnsafeUpdates = true;" command. I would prefer not to allow unsafe updates and would recommend you keep this line uncommented, but I have left it in in order to give you an option if you find that this still does not work properly in your situation.
This is generally because in order to move (rename) a file in Sharepoint, the user must have "delete" permission on list.
Provided the users have enough permission, then the code below works. The trick here is to use an SPFile object to update the "Name" property, whilst using an SPListItem object to control the files checkin/out status.
Keen readers will see that I am doing a ValidateFormDigest() in order to prevent the "Page Validation" error if this is caused from a form's POST action, but there is also a "web.AllowUnsafeUpdates = true;" command. I would prefer not to allow unsafe updates and would recommend you keep this line uncommented, but I have left it in in order to give you an option if you find that this still does not work properly in your situation.
using (SPSite site = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb web = site.OpenWeb()) { try { SPUtility.ValidateFormDigest(); SPListItem target = web.GetListItem(fileUrl); // fileURL should be set to the full path of the document you are changing if (target.File.CheckOutType == SPFile.SPCheckOutType.None) { //web.AllowUnsafeUpdates = true; // Shouldn't need this with the validateformDigest try { target.File.CheckOut(); SPFile filet = web.GetFile(target.Url); filet.Item["Name"] = newName; // newName is the new name of the file target.Update(); } catch (Exception ex) { web.AllowUnsafeUpdates = false; target.File.UndoCheckOut(); ShowErrorMessage("Error validating document: " + ex.Message); // Deal with error return; } target.File.CheckIn("", SPCheckinType.MajorCheckIn); // Make major version web.Update(); web.AllowUnsafeUpdates = false; } catch (Exception ex) { ShowErrorMessage("Error: " + ex.Message); //Deal with error //throw; } finally { web.AllowUnsafeUpdates = false; } } }
Labels:
sharepoint,
Visual Studio
Friday, 1 March 2013
Friday, 19 October 2012
SharePoint workflow - Delay activity never ends, workflow waits forever.
![]() |
Carissima Gold 9 ct Yellow Gold Two-Row Diamond Cut Curb Bracelet of 21 cm/8.5-inch |
By far the most painful part I have come across is the dreaded delay activity never waking up problem. This occurs when you place a delayActivity object in your workflow in order to wait for something to happen (for example in a while loop). This is pretty fundamental stuff in a workflow, so you would expect this to be a basic requiredment, but for some reason SharePoint has a HUGE flaw in this department: when you are developing your workflows, they seem to get stuck in the delay and never wake up!
![]() |
A WWF DelayActivity in a while loop |
![]() |
Workflow history - it never wakes up after delay :( |
After much head scratching, I have found a solution. The delayActivity passes control to the SharePoint Services Timer (OWSTIMER.EXE), and this gets confused if you redeploy the wrokflow.
To fix, follow these instructions.
First run this command on your farm:
(http://technet.microsoft.com/en-us/library/cc424946%28v=office.12%29.aspx)
(note apparently doesn’t like being anything but 5 minutes! )
I rebooted my dev box after this but this is probably not required.
The key part is that now provided I restart the Microsoft Sharepoint Timer service between deploys, it actually completes a delay! So remember to recycle the "Sharepoint 2010 Timer" service before you depoly a new version of the workflow to the farm.
![]() |
OWSTIMER service |
Note that this was supposedly a bug fixed in 2007 that is back in 2010. See this hotfix for 2007 systems: http://www.microsoft.com/en-us/download/details.aspx?id=21066
To debug any code that runs after a delay, you have to attach Visual Studio to the timer service (owstimer.exe), not the w3w process. I do this by switching off "auto retract after debugging" in the "Sharepoint" page of the project's properties file and manually attaching to the OWSTIMER.EXE process.
Labels:
delayActivity,
sharepoint,
Workflow
Friday, 8 June 2012
Unexpected Error when deploying web part pages using Visual Studio solution
It is good practice to wrap any Sharepoint web part pages you develop in a feature, and deploy them as a solution using Visual Studio. There are many walkthroughs on how to do this on the net, including this excellent Microsoft effort so I won't go into the details here.
If you follow this technique, the basic steps to developing a deployable feature are:
Note that my solution includes much more than just a web part page - it also has a masterpage, many images, CSS and javascript/jQuery - in other words all the things you need to make a nice custom web part page all in one deployable feature.
So everything is fine, but then you deploy the solution, and ARGHH! it throws an error! Worse, it throws an "Unexpected Error". This is the least useful error message you can imagine. So whats the solution?
Well after going through all the XML for my features I found the following:
The highlighted line is:
This is the ID of the web part file WHEN IT WAS EXPORTED.
When you deploy this solution to a site, the ID of the file is whatever the next ID available (it's basically an incremental integer). Therefore by leaving in this piece of declarative XML, we are attempting to force the file to use this ID. This is what is causing the "Unexpected error" on deployment.
Simply comment out this line and your solution will deploy.

If you follow this technique, the basic steps to developing a deployable feature are:
- Make your web part page in the Sharepoint front end or Sharepoint designer
- Once development complete, export the page to a site solution file
- Copy the appropriate section to your Sharepoint deployment soluting project
Note that my solution includes much more than just a web part page - it also has a masterpage, many images, CSS and javascript/jQuery - in other words all the things you need to make a nice custom web part page all in one deployable feature.
So everything is fine, but then you deploy the solution, and ARGHH! it throws an error! Worse, it throws an "Unexpected Error". This is the least useful error message you can imagine. So whats the solution?
Well after going through all the XML for my features I found the following:
The highlighted line is:
When you deploy this solution to a site, the ID of the file is whatever the next ID available (it's basically an incremental integer). Therefore by leaving in this piece of declarative XML, we are attempting to force the file to use this ID. This is what is causing the "Unexpected error" on deployment.
Simply comment out this line and your solution will deploy.

Labels:
deploy,
sharepoint,
Unexpected Error,
Visual Studio,
webpart
Friday, 25 November 2011
Integration between Word 2003 and SharePoint 2010 using Managed Meta-data
You cannot check out/edit/check in directly from Word 2003 if you are using managed meta-data in the list. Word 2003 does not seem to integrate properly with managed meta-data columns, and will refuse to check in the document.
This leaves teh document in an unsaved, checked out state if a user attempts to edit it directly from Sharepoint.
My only solution to this has been to create a new content type and remove the managed meta-data from it.
This leaves teh document in an unsaved, checked out state if a user attempts to edit it directly from Sharepoint.
My only solution to this has been to create a new content type and remove the managed meta-data from it.
![]() |
Carissima Gold 9 ct Yellow Gold Two-Row Diamond Cut Curb Bracelet of 21 cm/8.5-inch |
Labels:
Check in,
Managed Meta-Data,
sharepoint
Friday, 4 November 2011
C# function to get the Content Type GUID from a Sharepoint list using web services
Simple helper function to get the GUID of a list from Sharepoint web services.
Note that in the code below I have a class level object pointing to the Lists.asmx web service called m_listService already established , but I have left in the code to establish this connection in the comments as an example.
Note that in the code below I have a class level object pointing to the Lists.asmx web service called m_listService already established , but I have left in the code to establish this connection in the comments as an example.
string GetListContentTypeGuid(string listName, string listID, string contentTypeName) { string defaultGuid = ""; try { //ListsService.Lists listService = new ListsService.Lists(); //listService.Credentials = System.Net.CredentialCache.DefaultCredentials; //XmlNode ndList = listService.GetList(fileInfo.m_listInfo.m_listName); //XmlNode ndVersion = ndList.Attributes["Version"]; XmlNode contentTypes = m_listService.GetListContentTypes(listID, "anything"); // Loop through the fields foreach (XmlNode node in contentTypes) { System.Diagnostics.Debug.Print(node.Attributes["Name"].Value.ToString()); if (node.Attributes["Name"].Value.ToString() == contentTypeName) { defaultGuid = node.Attributes["ID"].Value.ToString(); break; } } } catch (Exception ex) { throw new Exception("ERROR: Reading content types from target site.\r\n" + ex.Message + "\r\nDetails: " + ex.InnerException + "\r\n" + "Check the settings file to ensure that the list settings match the target site.", ex); } return defaultGuid; }
Labels:
Content Type,
GUID,
sharepoint,
web services
Subscribe to:
Posts (Atom)