26

When I created a new universal app project in Visual Studio it created a shared project that let me share code between the Windows Phone 8.1 and Windows 8.1 projects that were created.

Now I have other projects that I would also like to use that shared code. However, I do not see a way to add select that project in the "Add Reference..." window.

If I try to copy the reference from one of the existing projects I get the error:

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

when I click 'Paste Reference'. How do I reference the shared project from other projects?

Brandon
  • 645
  • 5
  • 13
vossad01
  • 11,552
  • 8
  • 56
  • 109

5 Answers5

40

Adding the reference will require editing the project files where you want to add it. If it helps, you can peek at the project file where it is already referenced to see a working example.

Near the bottom of the project file (ex, a .csproj) there is likely already an <Import> element such as

<Project ...>
  [...]
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

You add the Shared project by adding another element like that for the Shared project. For example:

<Project ...>
  [...]
  <Import Project="..\Shared\Shared.projitems" Label="Shared" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

It is important for the Label attribute to be set to "Shared." If you set it to something else it will not be recognized as a Shared project by Visual Studio and will not appear under References. Project should be set to the path to the appropriate ".projitems" file.

spinjector
  • 3,121
  • 3
  • 26
  • 56
vossad01
  • 11,552
  • 8
  • 56
  • 109
  • 1
    It would be nice to see here, how to add a reference to a dll as well. – Jarekczek Nov 01 '16 at 19:12
  • 2
    I wish that in visual studio 2017, you didn't have to hack the .csproj file – Omzig Jan 17 '18 at 22:43
  • @Omzig does [@theguy's answer for VS2017](https://stackoverflow.com/a/46131355/1072626) not work? I can't test it. – vossad01 Jan 17 '18 at 23:17
  • Correct Vossad01, the "Shared Projects" tab is missing. Unless you have to do something dumb and enable it. – Omzig Jan 18 '18 at 14:49
  • Apparently my problem with shared projects has something to do with the Unit Tests that I am running. https://developercommunity.visualstudio.com/content/problem/184544/shared-projects-missing.html – Omzig Jan 24 '18 at 15:13
  • 1
    I had the issue in VS 15.6.6 at a webapi2 project. So it's not limited to unit testing. – JP Hellemons Apr 12 '18 at 14:30
  • @JPHellemons I like your suggestion the best. That way I can navigate to the shared project on my dev machine, add it as an existing reference and let Visual Studio handle how it puts that into the solution file. Then go to whatever project I want to use the shared project from and an a reference to the imported shared project. – Rod Apr 09 '20 at 20:19
10

Visual Studio 2017:

Right-click the References or Dependencies item in the Solution Explorer and choose "Add Reference..."

Dependencies or References context menu

The Reference Manager will open. Click "Shared Project" on the left side of the Reference Manager

enter image description here

Then select your project and click OK.

theguy
  • 861
  • 9
  • 19
  • 4
    This *should* theoretically work for VS2015 as well (the same tabs are all present). However I ran into the problem where the shared project was not listed for some reason. – Mage Xy Oct 03 '17 at 21:49
  • 5
    @MageXy are you sure these are visual studio 2017 screenshots? my VS2017 doesn't have "Shared Projects" /grr – Omzig Jan 19 '18 at 16:40
  • 1
    I do not see "Shared Projects" in the Reference Manager in VS2017 – Matthew Jun 05 '18 at 15:44
  • I am using VS 2017 and do have the Shared Projects on the left side of the Reference Manager. However, I cannot find a way to add the Shared Project. I clicked on the Browse... button, then tried added the .sln file, that failed. Then I tried adding the .shproj file, that failed. – Rod Mar 27 '19 at 20:31
  • Check your version of VS 2017. There was a bug fix in 15.9 that will show the Shared Projects selection – JoeyD Apr 02 '20 at 16:57
3

In TargetProject.csproj file add that string:

<Import Project="..\YourSharedProject\YourSharedProject.projitems" Label="Shared" Condition="Exists('..\YourSharedProject\YourSharedProject.projitems')" />
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • This worked for me. VS2019 - If I added an "existing" project to the solution, only then would it allow me to add a "shared" project reference to the target. This is through the GUI. If I just added a line like the one above directly to the target csproj, it worked, too. – Bill Noel Dec 07 '20 at 16:50
0

Check your version of Visual Studio. in VS2017 there was a bug fix for version 15.9 which will show the Shared Project selector. Click Tools -> Update to get the lastest version of VS

JoeyD
  • 693
  • 4
  • 25
-3

Check out the Shared Project Reference Manager extension.

hortman
  • 1,086
  • 10
  • 11