Category Archives: SharePoint

Troubleshooting 405 Error when Deploying Apps to SharePoint 2013 Online from Visual Studio

Scenario

You create a new SharePoint 2013 online tenant for development purposes. You want to deploy a ‘provider-hosted’ app from Visual Studio (more info on apps here). You receive the following error when running/deploying the app:

Error occurred in deployment step ‘Install app for SharePoint’: The remote server returned an unexpected response: (405) Method Not Allowed.

image

Proposed Workaround

To solve this issue you first need to register your SharePoint app by browsing to the following page: http://{yourSharePointServerName}/sites/{yourSiteName}/_layouts/15/appregnew.aspx. You should fill Client Id and Secret with the values you are already using in your app or Generate new ones. Additionally, you need to fill the Title, App Domain and Redirect URI. If you are running the SharePoint app in your dev environment (i.e. localhost) make sure the port is included in the App Domain. Also make sure that the Redirect URI is HTTPS.

You can lookup app registration information for the app that you have registered in this page http://{yourSharePointServerName}/_layouts/15/appinv.aspx. To do a lookup, you have to use the client Id (also known as the app Id). You can learn more about this process in this article: Guidelines for registering apps for SharePoint 2013.

image

If you, by any chance, make a mistake when defining the values, and you want to change them after you clicked Create, then you are lost Confused smile. At least, I couldn’t find a way to update the values.

I assumed the following procedure *would* work, but it didn’t.

  1. Open the http://{yourSharePointServerName}/_layouts/15/appregnew.aspx page.
  2. Complete the App Id and clicking Lookup.
  3. Complete the Permission Request XML with the <AppPermissionRequests> value from AppManifest.xml of the SharePoint project. For example:
  4. <AppPermissionRequests AllowAppOnlyPolicy=”true”>
      <AppPermissionRequest Scope=”http://sharepoint/content/sitecollection/web” Right=”Manage” />
      <AppPermissionRequest Scope=”http://sharepoint/search” Right=”QueryAsUserIgnoreAppPrincipal” />
    </AppPermissionRequests>

  5. Click Create.

If you perform the lookup again, you will find that the values remain the same.

 

image

Installing SharePoint 2013: Failed to Create Sample Data (NullReferenceException) Issue

I was getting the following error when installing SharePoint 2013 in the stand-alone configuration over a clean Windows Server 2012 virtual machine:

Failed to create sample data.

An exception of type System.NullReferenceException was thrown. Additional exception information: Object reference not set to an instance of an object.

After testing without luck all of the approaches explained in these links:

The one that finally solved it was: Installing all Windows Updates and re-running the Configuration Wizard.

image1

Another Tip: Do not start installing SharePoint 2013, without installing first .NET Framework 3.5 manually from Windows Features first. If you are unable to install it because of this error:

“The source files could not be found”

It is because you need to specify the Windows Server Installation disc – check this post or this one for more information.

Happy SharePoint coding! 🙂

image2 image3

Updating the Web.config in SharePoint Applications (The Right Way)

Some time ago, I needed to create and register a custom HttpModule in a SharePoint Web Application. As you probably, know registering the module implies adding an entry in the <modules> section of the Web.config. Reviewing SharePoint Best Practices, I’ve learned that the recommended way of doing this in a SharePoint Application is through the use of the SPWebconfigModification class. If used correctly, this class allows you to update the Web.config automatically while deploying the solution, without needing to modify the Web.config manually in each Web Front End (WFE).

To have SharePoint perform the update during deployment, you have to use the SpWebconfigModification class within a Feature Receiver of your SharePoint solution. The Feature Receiver allows you to write code to be executed when your WSP (SharePoint Solution Package) is installed/uninstalled.

Some comments from the Community show that there are some known issues with the SpWebconfigModifications class under certain conditions. However, don’t get discouraged! So far, I’ve tested it for registering a new HttpModule and it has worked successfully, reducing deployment times considerably.

Some Links

I won’t get into the details of the SpWebconfigModifications class, but for a deeper insight, here are some links I’ve collected:

Some Pictures

As a quick reference, in the following picture you can see the SPWebConfigConfiguration Class within the SharePoint FeatureReceiver:

event.receiver

The SpWebconfigModification is applied in the FeatureActivated event of the Feature Receiver.

event.receiver.2

The following pictures show the FeatureReceiver within the SharePoint solution and how to add it:

event.receiver.3

image

The Code

Here you can find the code of the FeatureReceiver using the SPWebconfigModification class to register an HttpModule. Some considerations:

  1. As with any other SharePoint assembly the HttpModule needs to be signed. I somehow struggled ‘a bit’ on how to get the HttpModule assembly strong name. I ended using PowerShell with this line:

    [System.Reflection.AssemblyName]::GetAssemblyName({assemblyPath}).FullName

  2. Make sure that you are modifying the Web.config of an SPSite and not an SPWebApplication. In that case, replace this line:

    SPSite site = properties.Feature.Parent as SPSite;

    with this one:

    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

  3. In other posts I’ve seen the following line:

    SPFarm
    .Local.Services.GetValue<SPWebService>(webApp.Parent.Id).ApplyWebConfigModifications();

    Instead of this one:

    site.WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    I’m still not sure what the difference is, but the only one that worked for me is the last one (site.WebApplication…) . I suspect that it depends on whether or not you are running SharePoint within a farm.

Here comes the code…

namespace GSK.RD.EDX.AccessImprovement.Features.TrainingAppFeature
{
    using System;
    using System.Collections.ObjectModel;
    using System.Runtime.InteropServices;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    [Guid("3ed0ded5-8203-4fc1-a372-e021af403237")]
    public class TrainingAppFeatureEventReceiver : SPFeatureReceiver
    {
        private const string cWebConfigModificationOwner = "TrainingAppFeature.AuthenticationRedirectHttpModule";

        SPWebConfigModification spWebConfigModification = new SPWebConfigModification()
        {
            // The owner of the web.config modification, useful for removing a 
            // group of modifications
            Owner = cWebConfigModificationOwner,
            // Make sure that the name is a unique XPath selector for the element 
            // we are adding. This name is used for removing the element
            Name = "add[@name="AuthenticationRedirectHttpModule"]",
            // We are going to add a new XML node to web.config
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
            // The XPath to the location of the parent node in web.config
            Path = "configuration/system.webServer/modules",
            // Sequence is important if there are multiple equal nodes that 
            // can't be identified with an XPath expression
            Sequence = 0,
            // The XML to insert as child node, make sure that used names match the Name selector
            Value = "<add name="AuthenticationRedirectHttpModule" type="RD.EDX.AccessImprovement.AuthenticationRedirectModule.AuthenticationRedirectHttpModule, GSK.RD.EDX.AccessImprovement.AuthenticationRedirectModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=526edf2f87bc2a9e" />"
        };

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            
            SPSite site = properties.Feature.Parent as SPSite;            
            if (site != null)
            {                
                site.WebApplication.WebConfigModifications.Add(spWebConfigModification);

                // Commit modification additions to the specified web application
                site.WebApplication.Update();

                // Push modifications through the farm
                site.WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();               
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                Collection<SPWebConfigModification> modificationCollection = site.WebApplication.WebConfigModifications;
                Collection<SPWebConfigModification> removeCollection = new Collection<SPWebConfigModification>();

                int count = modificationCollection.Count;
                for (int i = 0; i < count; i++)
                {
                    SPWebConfigModification modification = modificationCollection[i];
                    if (modification.Owner == cWebConfigModificationOwner)
                    {
                        // collect modifications to delete
                        removeCollection.Add(modification);
                    }
                }

                // now delete the modifications from the web application
                if (removeCollection.Count > 0)
                {
                    foreach (SPWebConfigModification modificationItem in removeCollection)
                    {
                        site.WebApplication.WebConfigModifications.Remove(modificationItem);
                    }

                    // Commit modification removals to the specified web application
                    site.WebApplication.Update();

                    // Push modifications through the farm
                    site.WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                }
            }
        }
    }
}

SQL Server 2008 R2 Update for Developers Training Kit (June 2010 Update) – RTM Refresh Released!

SQL Server 2008 R2

Today Roger announced in his blog the availability of the SQL Server 2008 R2 Update for Developers Training Kit (June 2010 Update). This is the final update for this training kit and includes two additional modules covering SQL Server 2008 R2 related improvements in Excel 2010 and SharePoint 2010. Additionally, there were many updates to the original content in previous versions of the training kit to address changes in the final RTM release of SQL Server 2008 R2 (e.g. some UI changes, modifications in the API, minor bug fixing, etc).

You can download this new release at:

New modules added to the Training Kit

Excel 2010 & Sharepoint 2010

As I said previously, the Excel 2010 and SharePoint 2010 modules were included in the RTM release related with SQL Server 2008 R2 improvements:

  • Excel 2010 Module Excel 2010: Excel is the most popular data analysis tool in the world, and Excel 2010 offers a range of powerful data analysis and visualization improvements that makes it the ideal self-service analysis client. Slicers are new visual controls that provide quick, easy and interactive filtering. Sparklines are intense, simple word-sized graphics that make it easy to visualize data in-line with numbers. PivotTables support powerful new capabilities like named sets, what-if analysis (write-back), and pre-built calculations like percent of total, running totals and ranks. Charting has also been improved with an easier user interface, faster performance and expanded limits. SQL Server 2008 R2 introduces PowerPivot for Excel, a free Excel add-in that makes it possible for information workers to create their own powerful analytic models by combining and relating data from a wide variety of data sources.
  • SharePoint 2010 Module SharePoint 2010: SharePoint 2010 is the most popular business collaboration platform in the world for organizations of all sizes, and SharePoint 2010 Insights lets everyone access the business information they need to make good decisions. SharePoint 2010 Insights uses well-known applications and interfaces, people are comfortable from the start, and they know how to get the data they need. Excel Services makes it easy for anyone to publish their Excel workbooks in SharePoint 2010, including workbooks that use data from SQL Server 2008 R2 Analysis Services. From there, an entire team can access and analyze the same data from their web browser and rest assured that everyone has the right information. PerformancePoint Services is a new integrated capability in SharePoint 2010 Enterprise Edition that makes it easy to build flexible, highly interactive scorecards and dashboards using data from SQL Server 2008 R2 Analysis Services. SQL Server 2008 R2 PowerPivot for SharePoint makes it easy to publish analytic models created with SQL Server PowerPivot for Excel to SharePoint 2010 and share them with other users with Excel Services. SQL Server 2008 R2 Reporting Services supports SharePoint 2010 integration, making it easy to publish and access reports using familiar SharePoint paradigms.

Requirements

Enjoy

Mariano

How to: Enable CWAB in a SharePoint application guidance released

A couple of weeks ago a new guidance to enable the Composite Web Application Block in a SharePoint application has just been released by the Patterns & Practices Sustained Engineering team. You can download the release from the Web Client Software factory codeplex site.

Enabling CWAB in Sharepoint applications will allow developers to use features like dependency injection.

The main responsibility of enabling CWAB in Sharepoint applications lays with the SPWebClientApplication and the RootContainerLocatorService classes.

While the SPWebClientApplication’s BuildItemWithCurrentContext method runs different code depending on the application implementation (WebClient/Sharepoint), hence allowing Web Part reusability, the RootContainerLocatorService always returns the same instance of the root CompositionContainer.

This is the code of the BuildItemWithCurrentContext method:

public static void BuildItemWithCurrentContext(object obj)
{
   IHttpContext context = CurrentContext;
   if (context.ApplicationInstance is SPWebClientApplication)
   {
      SPWebClientApplication app = (SPWebClientApplication)context.ApplicationInstance;
      IBuilder<WCSFBuilderStage> builder = app.PageBuilder;
      CompositionContainer container = app.GetModuleContainer(context);
      CompositionContainer.BuildItem(builder, container.Locator, obj);
   }
   else if (context.ApplicationInstance is WebClientApplication)
   {
      WebClientApplication.BuildItemWithCurrentContext(obj);
   }
}

As mentioned in my post about the guidance to Enable Unity in Sharepoint, the Model-View-Presenter pattern is used in this example. The objective of this pattern is placing all complex UI logic in a presenter class, thus making the  easier to test. The image below (from the MSDN Web Client Software Factory site) shows the relationship between the different components in the pattern.

MVP

We also posted the complete guidance here for online reference. It contains the following topics:

  • How To: Enable CWAB in a SharePoint Application
  • How To: Reuse WCSF User Controls in SharePoint
  • How To: Use CWAB to Build Testable Sharepoint Web Parts
  • How To: Reuse Foundational Modules in Sharepoint

If you want more info about SharePoint you can check:

Hope you find the guidance useful.

Enabling CWAB and Unity in SharePoint applications sets of guidance published

The Patterns & Practices Sustained Engineering has just published two sets of guidance for SharePoint to make Web Parts more testable. The approach is enabling a dependency injection container in SharePoint applications. You can download these new releases from here:

Each of the releases include the following elements:

  • A PDF document with guidance to enable CWAB/Unity in a SharePoint application to make Web Parts more testable.
  • Extensions for CWAB/Unity to work on a SharePoint application
    • CompositeWeb.SharePoint solution: it includes a class that implements the IWebClientApplication interface and inherits from SPHttpApplication class.
    • Unity.SharePoint solution: it includes a class that inherits from SPHttpApplication and hosts a Unity container.
  • A sample implementation that uses the extensions for CWAB/Unity. This solution includes a Web Part that is injected with dependencies (also has a Unit Test project).
  • Sample files like the global.asax and web.config for a SharePoint application enabled with CWAB/Unity.

SharePoint Releases

We also published the documentation guidance in the WCSF KB as wiki pages. You can find them here:

Enjoy

How To: Enable Unity in a SharePoint application guidance just released

A new guidance to help developers enable Unity in a SharePoint application has just been released by the Patterns & Practices Sustained Engineering team. You can download the release from the Web Client Software factory codeplex site.

In the release, the base SPHttpApplication class is customized by using it as the parent class of the SPUnityHttpApplication class.

The main feature this class has, to enable the Unity container, is the BuildItemWithCurrentContext method. Below is the code for this method:

public static void BuildItemWithCurrentContext(object obj)
{
      HttpContext context = System.Web.HttpContext.Current;
      Unity.SharePoint.SPUnityHttpApplication app =
         (Unity.SharePoint.SPUnityHttpApplication)context.ApplicationInstance;
      app.Container.BuildUp(obj.GetType(), obj);
}

The objective of  enabling the Unity container is holding the different elements of your application to perform dependency injection into your classes.

Custom Web Part and User Control classes are created in the release to enable them for Dependency Injection. This will give your classes a low coupling level, since there will be no hard references between them. Given that, you can perform unit tests in an easy manner.

To increase the testing capabilities even more, the Model-View-Presenter pattern is used in this example. The objective of this pattern is placing all complex UI logic in a presenter class, thus it is easier to test. The image below (from the MSDN Web Client Software Factory site) shows the relationship between the different components in the pattern.

MVP

We also posted the complete guidance here for online reference. It contains the following topics:

  • How To: Enable Unity in a SharePoint Application
  • How To: Create a Web Part that uses Dependency Injection
  • How To: Unit test the Web Part
  • How To: Deploy the Web Part

If you want more info about SharePoint you can check:

The next post will cover the guidance to enable CWAB in a SharePoint Application, stay tuned.