1

I am using sharepoint 2010 with some custom timer job at web application scope. In this timer job I have a foreach which loops into all site collection in the current web application. There is an if where I check if there is some custom feature activated. I would like to change this code. It needs to check this only on the root site collection of the web application. Not the sub site collections. How can I do this?

public override void Execute(Guid targetInstanceId)
    {
        try
        {
            DiagnosticsService.LogMessage(this.Title + " - Started", 0, DiagnosticsCategory.Jobs);

            var webApplication = this.Parent as SPWebApplication;
            if (webApplication == null)
            {
                DiagnosticsService.LogError("Web application not found", 0, DiagnosticsCategory.Jobs);
                return;
            }

            foreach (var site in webApplication.Sites.ToList())
            {
                try
                {
                    // TODO 1 : check if this is the root site collection of the web application
                    if (site.Features[ProfileTranslationListsDefinitionFeatureId] != null)
                    {
                        this.ProcessProfiles(site);
                    }
                }
                finally
                {
                    if (site != null)
                    {
                        site.Dispose();
                    }
                }
            }
  • 1
    There are no such thing as a Root Site Collection, there is really no relations at all between different site collections. Do you mean the site collection that has the same URL as your DNS entry? – Robert Lindgren Nov 14 '13 at 15:39
  • 1
    Maybe you are confusing Root Site Collection with the "/" site collection that is a Top Level Site Collection. – Alvmad Nov 14 '13 at 16:47
  • If I have a web application with this url "portal.mycompany.com" then I see the root site collection. Inside this site collection I can add a sub site collection for example "portal.mycompany.com/location/amsterdam". This sub site collection I dont want to get. I would like to get only the root site collection. – Ola Nov 14 '13 at 18:28
4

You can use LINQ to filter the Sites collection on the web application. Something like:

webApp.Sites.Where(p => p.ServerRelativeUrl.Equals("/", StringComparison.OrdinalIgnoreCase));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.