none
How to get SPSite url by Sites.First<SPSite>(); RRS feed

  • Question

  • I am using the below mentioned code to get the my web site url but i am getting below mentioned error.whether any other method available to fetch the site url ?

    Error:

    Microsoft.SharePoint.Administration.SPSiteCollection  doesn't contain the definition First.

    Code:

    Microsoft.SharePoint.Administration.SPWebApplication webapp = SPContext.Current.Site.WebApplication;
     SPSite site = webapp.Sites.First<SPSite>();

    Monday, December 24, 2012 6:38 PM

Answers

  • You posted this in "SharePoint Legacy Versions". Which version of SharePoint are you using?

    It looks like you are trying to use LINQ, this will only work in .Net Framework 3.5, so most likely you are working in SP 2010.

    Did you add a reference and a using statement for LINQ?     using System.Linq;

    Are you compiling to framework 3.5 and have the build set to x64 or Any CPU? (required for SP2010)

    This code worked for me:

    SPWebApplication app = SPWebApplication.Lookup(new Uri("http://sharepoint/sites/Testing"));
    SPSite site =  app.Sites.First<SPSite>();
    Console.WriteLine(site.Url);
    

    This also worked: app.Sites.First();


    Mike Smith TechTrainingNotes.blogspot.com my SP customization book

    • Marked as answer by nsksaravana Tuesday, December 25, 2012 1:06 AM
    Monday, December 24, 2012 9:13 PM

All replies

  • You posted this in "SharePoint Legacy Versions". Which version of SharePoint are you using?

    It looks like you are trying to use LINQ, this will only work in .Net Framework 3.5, so most likely you are working in SP 2010.

    Did you add a reference and a using statement for LINQ?     using System.Linq;

    Are you compiling to framework 3.5 and have the build set to x64 or Any CPU? (required for SP2010)

    This code worked for me:

    SPWebApplication app = SPWebApplication.Lookup(new Uri("http://sharepoint/sites/Testing"));
    SPSite site =  app.Sites.First<SPSite>();
    Console.WriteLine(site.Url);
    

    This also worked: app.Sites.First();


    Mike Smith TechTrainingNotes.blogspot.com my SP customization book

    • Marked as answer by nsksaravana Tuesday, December 25, 2012 1:06 AM
    Monday, December 24, 2012 9:13 PM
  • Thanks a lot. This will not work in the sanbbox solutions ? in sandbox how can i achieve it ?
    Tuesday, December 25, 2012 1:06 AM
  • Sanbox solutions are limited to a single site and the permissions of the currently logged in user. What is it that you are trying to do?


    Mike Smith TechTrainingNotes.blogspot.com my SP customization book

    Tuesday, December 25, 2012 4:54 AM
  • I need a generalized code for the below mentioned code. please help me.

    using (SPSite loSite = new SPSite("http://win-k1rvsv7r5en:2222/"))

    Tuesday, December 25, 2012 7:31 AM
  • What is "generalized code"?


    Mike Smith TechTrainingNotes.blogspot.com my SP customization book

    Tuesday, December 25, 2012 7:17 PM
  • Code http://win-k1rvsv7r5en:2222/ is my site URL and if i want to implement in some other server i cann't use the same and the URL will be different. So if i use some generalized code i.e take the URL according to the Site URL and i no need to change the code. My project is in the sandbox environment.
    Wednesday, December 26, 2012 6:15 AM