Archive

Archive for the ‘Paul Busby’ Category

How to validate string.format within Visual Studio

February 28, 2014 Leave a comment

 

In writing code we use string.format a lot to create strings for error reporting or sending to the screen. Some of these will only ever get created if an error occurs so they become difficult to test to make sure the don’t throw an error. Take the following example

The following code would fail at runtime but is fine in the compiler

string.Format("{0} This is valid but will error {1}", this.siteURL);

So how do we catch these. Well we all use the code analysis tools built into visual studio. Don’t we?

 

Well this has a nice test case that will catch and highlight this problem.

 

Run code analysis on your project Alt+F11 or select “Run Code Analysis on …” from the Analyse menu.

 

Then once this has finished you can search for issue CA2241. This will show any and all string.formats that would fail at run time.

 

It would be nice if the compiler caught these but this is still a good approach.

via Buzz Blog http://bit.ly/1hYG3FD

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Infopath Namespace manager creation code

February 18, 2014 Leave a comment

I have had to create some code this week that reads the xml created by InfoPath. I then found that if you try and xpath on something you get a null return until the namespace manager has all of the correct entries. In the past I have created these manually but after a bit of google fo I have come up with the below piece of code. It takes an XMLDocument that you have created based on the form file (or any XML document really) and returns you a newly created namespace manager that can be used along side you xpath queries to get the data you want.

 

 

public static XmlNamespaceManager GetNameSpaceManager(XmlDocument xmlDoc)
{
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
foreach (XmlAttribute att in xmlDoc.DocumentElement.Attributes)
{
if (att.Prefix == "xmlns")
{
ns.AddNamespace(att.LocalName, att.Value);
}
}
return ns;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

via Buzz Blog http://bit.ly/MuMAtl

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

How to hide fields for new items using InfoPath

January 23, 2014 Leave a comment

 

A client had a fairly simple request to hide some fields when a new SharePoint list item is created but have these fields shown once the item has been created and people edit the form. The short version is that you can use the inbuilt ID field to see if an item is new or not. If the ID field is blank then it is a new form.

 

Here are the steps required so that you can hide fields when an item is being created. Make sure not to hide any required fields or the form cannot be saved.

These steps require InfoPath 2010 client to be installed

1. Navigate to the list

a. I have created a very simple list where users will ask a question. The goal is only to allow users to fill out the title field when an item is created

 

2. From the ribbon select the Customize Form option

a. clip_image001

b. If this option is missing then following feature is probably not activated

c. clip_image002

3. A form will now have opened in InfoPath 2010

a. clip_image003

4. Each field can be hidden manually or all of the fields you want to hide can be added to a new section and then you just hide the section.

a. Select the Section control and add to the bottom of the form

b. Copy or cut the required rows or fields from the table into the section. It will look similar to the following screen shot

c. clip_image004

5. Now we need to add a rule to the section to hide it.

a. Select the Manage Rules option from the ribbon

b. clip_image005

c. Highlight the section (Click on it)

d. clip_image006

e. From the Rules menu on the left hand side select New and then Formatting.

i. Change the name to be Hide Section

ii. Click on the None under condition and change it to the following

iii. clip_image007

iv. Now select the Hide this control option

v. The whole rule should look like

vi. clip_image008

6. Using the publish button in the very top left of InfoPath publish you changes back to the server. clip_image009

a. You should receive an OK message once this is complete

7. Now navigate to the list and test to see if it has worked

Here is the new form

clip_image010

This is what the form looks like when an existing item is edited

clip_image011

This is just a very basic example. The overall style of the form can be changed using the formatting option within InfoPath.

Rule can also be applied directly to some fields so they do not have to be moved into a section if you do not want to. The attachment control must be added to a section as rules cannot be applied against this control directly.

via Buzz Blog http://bit.ly/LK0nvp

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Updating database collation

January 10, 2014 Leave a comment

Had some fun trying to update a database collation. I didn’t have to worry about the data i just needed to the collation changed so that some cross database scripts would still function in order to carry out some testing.

 

Here is a nice little script I found that will generate scripts to change the collation on the required columns. Some of it fails if primary keys or indexes are against the columns but this will do 90%.

 

SELECT
      CAST(o.table_name + ‘ -> ‘ + c.name AS VARCHAR(60))
    , ‘ALTER TABLE ‘ + o.table_name +
        ‘ ALTER COLUMN [‘ + c.name + ‘] ‘ +
        UPPER(t.name) + ‘(‘ +
        CASE
            WHEN t.name IN (‘nchar’, ‘nvarchar’) AND c.max_length != -1 THEN CAST(c.max_length / 2 AS NVARCHAR(10))
            WHEN t.name IN (‘nchar’, ‘nvarchar’) AND c.max_length = -1 THEN ‘MAX’
            ELSE CAST(c.max_length AS NVARCHAR(10))
        END + ‘) COLLATE ‘ + @collate +
        CASE WHEN c.is_nullable = 1
            THEN ‘ NULL’
            ELSE ‘ NOT NULL’
        END
FROM sys.columns c WITH (NOWAIT)
JOIN (
    SELECT
          table_name = ‘[‘ + s.name + ‘].[‘ + o.name + ‘]‘
        , o.[object_id]
    FROM sys.objects o WITH (NOWAIT)
    JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id]
    WHERE o.[type] = ‘U’
) o ON c.[object_id] = o.[object_id]
JOIN sys.types t WITH (NOWAIT) ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
WHERE t.name IN (‘char’, ‘varchar’, ‘text’, ‘nvarchar’, ‘ntext’, ‘nchar’)
    –AND c.collation_name != @collate
ORDER BY
      o.table_name
    , c.column_id

via Buzz Blog http://paulbuzzblog.wordpress.com/2014/01/10/updating-database-collation/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Changing the format of date parameters on SSRS 2012

January 10, 2014 Leave a comment

 

Had an issue today where the date parameters were being forced into US data format and not using any of the regional settings from the OS or SharePoint.

 

This has apparently been fixed in SQL 2012 SP1 CU5 update but i have not confirmed this.

This post http://www.tachytelic.net/2013/01/changing-the-format-of-date-parameters-on-a-sql-server-2012-reporting-services-report-that-is-sharepoint-integrated/

Has a workaround that required you to add a culture into the RSViewerPage.aspx located in

 

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ReportServer

 

or

 

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\ReportServer

in SharePoint 2013

 

You need to add UICulture="en-GB"

 

to the end of

<%@ Page language="C#" Codebehind="RSViewerPage.aspx.cs" AutoEventWireup="false" Inherits="Microsoft.ReportingServices.SharePoint.UI.RSViewerPage,Microsoft.ReportingServices.SharePoint.UI.ServerPages,Version=11.0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91" %>

 

 

The full string looks like

 

<%@ Page language="C#" Codebehind="RSViewerPage.aspx.cs" AutoEventWireup="false" Inherits="Microsoft.ReportingServices.SharePoint.UI.RSViewerPage,Microsoft.ReportingServices.SharePoint.UI.ServerPages,Version=11.0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91" UICulture="en-GB"%>

 

If you are using Windows 2012 i had to save the updated file to a different location and then copy the file in and gain the correct permissions.

via Buzz Blog http://paulbuzzblog.wordpress.com/2014/01/10/changing-the-format-of-date-parameters-on-ssrs-2012/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Better logging in SharePoint

October 1, 2013 Leave a comment

 

Just a very quick post so i can remember this post which describes 5 different approaches to custom logging within SharePoint.

 

http://spdevlab.com/2013/06/21/5-suggestions-to-implement-a-better-logging-in-sharepoint/

via Buzz Blog http://paulbuzzblog.wordpress.com/2013/10/01/better-logging-in-sharepoint/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Recursive rules in InfoPath–How to prevent

September 24, 2013 Leave a comment

 

A while ago I had a scenario in an InfoPath form where the user could either enter an amount or a percentage value. This value would then get added to a total already in the form. The requirement is that the user enters either value and the form then calculates the other one.

So if I enter a number then the percentage gets calculated. If I entered the percentage then the number value gets calculated.

So I quickly setup some rules so that when the value changed the percentage or the whole numbers gets calculated and updates the relevant field. This creates a loop of continually updated fields.

 

I had hoped that some clever InfoPath “stuff” would just make this work and I wouldn’t have to worry about the issue. This didn’t work and caused the form to fail or the fields would not update correctly.

 

The solution to this problem is that you need a third field to control the updates. Mine is called “Calculating” and is a simple Boolean field.

 

Now in the rules for Field1 (number) and Field2 (percentage) the first thing to do is to check that calculating = 0 then set calculating = 1 and then set your field. At the end set Calculating back to 0.

 

This is just a very simple implementation of a lock but it stops the InfoPath rules going crazy.

 

image

Pictures explain everything

via Buzz Blog http://paulbuzzblog.wordpress.com/2013/09/24/recursive-rules-in-infopathhow-to-prevent/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Programmatically Disable Event Firing on List Item Update in SharePoint 2010

September 19, 2013 Leave a comment

 

All credit to the original post here

 

The short version for my future reference is

 

Create a simple class

 

public classEventFiring : SPItemEventReceiver
    {
       public void DisableHandleEventFiring()
        {
           this.EventFiringEnabled =false;
        }

       public void EnableHandleEventFiring()
        {
           this.EventFiringEnabled =true;
        }
    }

 

Then use this to disable events

 

 using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("Custom");
                    SPListItem item = list.GetItemById(34);
                    item["Title"] ="Updated Successfully";
                    EventFiring eventFiring = newEventFiring();
                    eventFiring.DisableHandleEventFiring();
                    item.Update();
                    eventFiring.EnableHandleEventFiring();
                    Console.WriteLine("Updated Successfully");
                    Console.ReadLine();
                }
            }

via Buzz Blog http://paulbuzzblog.wordpress.com/2013/09/19/programmatically-disable-event-firing-on-list-item-update-in-sharepoint-2010/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

Always open Visual Studio 2012 in admin mode

September 13, 2013 1 comment

 

So i have just started again using windows server 2012 and Visual Studio 2012. I had forgotten that you always have to run as an admin or lots of things don’t work. After this i forgot this twice i figured there must be a better solution.

 

Google to the rescue and all credit to the answer from this stack overflow question. These steps work great.

In Windows 8, you have to right-click devenv.exe and select "Troubleshoot compatibility".

1. select "Troubleshoot program"

2. check "The program requires additional permissions"

3. click "Next", click "Test the program…"

4. wait for the program to launch

5. click "Next"

6. select "Yes, save these settings for this program"

7. click "Close"

via Buzz Blog http://paulbuzzblog.wordpress.com/2013/09/13/always-open-visual-studio-2012-in-admin-mode/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)

SharePoint Evolution Conference–things to remember / learn

April 18, 2013 Leave a comment

 

So today was the last of the 3 day Evolution conference that has been running at the QEII centre in Westminster. http://www.sharepointevolutionconference.com/ 

 

The whole conference has been fantastic with a very good selection of speakers

My highlights were Andrew Connell’s (http://www.andrewconnell.com/blog) Talk on single page applications or SPA’s. I am sure this will appear on his blog soon

the other was Chris O’Brien’s (http://www.sharepointnutsandbolts.com) talk on SharePoint App’s and why they make a lot of sense. He did a mock conversation, which i have gone through myself, on how SharePoint hosted apps are not powerful enough and we want to use c# in a provider hosted app which then leads down the rabbit whole of either Azure costs or the maintenance and DR of our own server. This all leads back to the question “What was wrong with SharePoint hosted apps anyway?”

 

The big take away from the conference was that there is not that much you cannot do with SharePoint hosted app if you really think about it. So we need to get outside of our nice comfy C# sofa and start learning JavaScript.

 

By this I mean REALLY learn JavaScript and not the hacking about we have been doing for the last few years. This means that we need to treat JavaScript as a real language (I realise that it is) and write the code properly using proper patterns. So that when we look at it all in a weeks time or have to show it in a demo it will make sense and that other people can actually support the code.

 

This seems daunting given the limited tooling that exists for JavaScript, especially when we think how lucky we are with the debugging tools we get for .NET code.

 

Someone, I forget who, came up with a brilliant analogy for what JavaScript is and it was “JavaScript is assembly language for the web”

When you think about it this makes a lot of sense. In the end the C# code we write is assembly when it finally gets executed.

 

This means that to write good JavaScript we need to use extra tools and frameworks that other people have provided.

 

The obvious one is jQuery which i now think is synonymous with JavaScript. If you are not using jQuery in you JS then you are just making life difficult.

 

The other frameworks that we should look to use and the main reason for this post are the following:

 

Using some or all of the above does make the learning a little more daunting but after seeing the results this is clearly the way to move forward. When you see how little code you actually have to write it is just amazing what can be done.

 

As well as the extra frameworks the other key JavaScript things to learn are namespaces and promises

 

Something else to note. Make sure you have the latest version of the SharePoint development tools installed and make sure you have the “Web Essentials 2012” extension installed as well. This will make life easier.

 

Please do not ask me for any code samples or video’s of the event as I do not have any and if I did I wouldn’t be allowed anyway.

via Buzz Blog http://paulbuzzblog.wordpress.com/2013/04/18/sharepoint-evolution-conferencethings-to-remember-learn/

Chris Stretton
Paul is a an expert SharePoint and Project Server developer and is responsible for designing and implementing custom solutions on client systems using the latest SharePoint and .NET technologies.
Paul has extensive experience with SharePoint systems across all sizes of implementation, ranging from small to large farms and has an excellent understanding of all the elements of SharePoint.

This article has been cross posted from paulbuzzblog.wordpress.com (original article)