Archive

Posts Tagged ‘sharepoint’

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/

Categories: work Tags: ,

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();
                }
            }
Categories: work Tags: ,

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.

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/

Infopath “The following form template cannot be upgraded because it is not currently uploaded on this farm” #sp2010

August 1, 2012 2 comments

So what i was trying to do is upload a new form template to a clients QA system. Something that has been done tens of time since the system went live. But today I get.

image

 

Now i have never seen this message before so i instantly start googling to try and save the day.

I have to say not a lot of stuff around this error is available but there are a couple of MSDN forum posts that got me in the right direction.

 

The SharePoint farm in question has recently had some more servers added to the farm and what i suspect has happened is that the form files have not correctly deployed to the new servers. But to prove this i need the feature GUID that is created when you first upload a InfoPath form.

 

I found the quickest way to get this is to navigate to the site collection the form exists on. Go to Site settings and under Site collection Administrators select the Site Collection Features in the list you will see a feature that maps to the name of the XSN file.

Click on the Deactivate button (don’t worry there is a confirmation step). Now in the query string the feature GUID is shown. Copy this as you will need it later. You don’t need to deactivate the solution so can close the page if you wish.

 

I was then able to see, under Solution Management within central admin the WSP called the same GUID. Clicking on the WSP showed me that the wsp had not been deployed to all the servers in the farm. For some reason all of the other WSP in the list had all been deployed. I guess i was just lucky that this was the only one.

 

Now to fix the issue you need to run an STSADM command

 

stsadm -o uninstallfeature –id <the feature GUID you got from the query string> –force

 

This will remove the feature and i guess some other magic that fixes stuff.

Now the form can be upgraded without an error. Once the form has upgraded the feature is reactivated automatically (did for me anyway)

And YAY it all works again. 5 minutes job only took an hour

 

Hope this helps someone

Categories: CPS, work Tags: , ,

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
}

Javascript Editor for SharePoint

This looks very cool indeed.

http://jefs.codeplex.com/documentation

Categories: work Tags: ,