Archive

Posts Tagged ‘visual studio’

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.

Categories: work Tags:

Always open Visual Studio 2012 in admin mode

September 13, 2013 Leave a 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"

Categories: work Tags: ,

Visual Studio 2012 templates for SharePoint 2013

January 23, 2013 2 comments

 

So I just fired up Visual Studio 2012 and went to create a new SharePoint 2013 App just to play around with. Only to find that Visual Studio only has the 2010 templates available.

 

The search to find out what I needed to install wasn’t as quick and simple as I was hoping so now that I have worked it all out i may as well share with the rest of you.

 

Assuming you have Visual Studio 2012 installed. Go to

http://msdn.microsoft.com/en-us/office/apps/fp123627

 

Scroll down and under the tools section there is a web downloader (Direct Link). Download and run it, it will download everything you are missing.

 

After a couple of restarts you can then create 2013 projects in visual studio

image

Hope that helps

Categories: CPS, work Tags: ,

Project Server workspace ProjectUID filter web part

August 10, 2012 1 comment

 

This is a great post containing a new filter web part that will send the projectGUID from a project server workspace to a web part.

http://epmsource.com/2011/10/09/projectuid-filter-provider-for-reporting-services-viewer-web-part/

Getting Project Server database information via C# / How to run PowerShell command in C#

July 31, 2012 2 comments

For a long time now I have had a few scenario’s where it would be helpful for me to know what the database server and database names are of a Project Server instance. The PSI does not provide this information but there is a PowerShell command that will give you the information.

Get-SPProjectWebInstance –url <URL>

This gives me all the info a want about the instance.

image

So now I know I can get the info i need i just need a way of getting this within c# code.

Helpfully you can use System.Automation to execute PowerShell scripts within .NET

We start with a simple method that will execute PowerShell commands

private string RunScript(string scriptText)
        {
            string addWss = "Add-PSSnapin microsoft.sharepoint.powershell";
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(addWss);

            pipeline.Commands.AddScript(scriptText);
            pipeline.Commands.Add("Out-String");
            var results = pipeline.Invoke();

            runspace.Close();

            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            return stringBuilder.ToString();
        }

The above code needs

using System.Management.Automation;
using System.Management.Automation.Runspaces;

NOTE: You’ll need to add a reference to the version of PowerShell you have installed in your GAC to the Visual Studio project file. To do so, you’ll open the project file as a text file and add the following line into the <ItemGroup> section:

<Reference Include=”System.Management.Automation” />

I was able to cheat and “browse” to the assembly using the following path

C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

Now that we have the helper method I can actually get the info I want.

string answer = RunScript("Get-SPProjectWebInstance -url http://vm641/pwa2");
string reportingDatabaseServer = string.Empty;
string reportingDatabaseName = string.Empty;

var output = answer.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Cast<string>();

reportingDatabaseServer = (from x in output where x.StartsWith("ReportingServer") select x).First().ToString();
reportingDatabaseName = (from x in output where x.StartsWith("ReportingDatabase") select x).First().ToString();

reportingDatabaseServer = reportingDatabaseServer.Replace("ReportingServer", "").Replace(":", "").Trim();
reportingDatabaseName = reportingDatabaseName.Replace("ReportingDatabase", "").Replace(":", "").Trim();

Bingo I have the name of the reporting server and the database name.

I needed this months ago to auto configure a PSI extension that was written but i need it now to auto configure a feature when it is deployed.

I’ll wrap all the above code into a single class that accepts the instance URL in its constructor and then expose properties with the information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Management.Automation;
using System.Text;

public class ProjectWebInstanceInformation
{
    #region Fields and Properties
    public string ReportingServer { get; private set; }
    public string ReportingDatabase { get; private set; }
    #endregion

    #region Constructor
    protected ProjectWebInstanceInformation()
    {
    }

    public ProjectWebInstanceInformation(string Url)
    {
        try
        {
            string answer = RunScript(string.Format("Get-SPProjectWebInstance -url {0}", Url));
            string reportingDatabaseServer = string.Empty;
            string reportingDatabaseName = string.Empty;

            var output = answer.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Cast<string>();

            ReportingServer = (from x in output where x.StartsWith("ReportingServer") select x).First().ToString().Replace("ReportingServer", "").Replace(":", "").Trim();
            ReportingDatabase = (from x in output where x.StartsWith("ReportingDatabase") select x).First().ToString().Replace("ReportingDatabase", "").Replace(":", "").Trim();
        }
        catch { }

    }

    #endregion

    #region Methods
    private string RunScript(string scriptText)
    {
        string addWss = "Add-PSSnapin microsoft.sharepoint.powershell";
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(addWss);

        pipeline.Commands.AddScript(scriptText);
        pipeline.Commands.Add("Out-String");
        var results = pipeline.Invoke();

        runspace.Close();

        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        return stringBuilder.ToString();
    }
    #endregion
}

Unit testing sharepoint 2010 in visual studio

December 6, 2011 Leave a comment

Found an issue today that you cannot use the SPSite object within your unit tests. This is due to the testing framework only working on .net4. You need to upgrade visual studio to SP1 and its all works as you would expect. Great post here http://www.sharepoint.bg/radi/post/Unit-Testing-SharePoint-2010-with-MSTest.aspx

How to: Modify the Ribbon in PWA

April 11, 2011 Leave a comment

Interesting looking MSDN artical

Check it out here

http://msdn.microsoft.com/en-us/library/gg549101.aspx

Categories: work Tags: , ,