Iterate through sub sites in a site collection

recently i was told to iterate through all the subsites and all the doc libs in a  site collection.

so, i had written the below code  in a  recursive way:

………………

getlistofwebsfromsitecollec(curresitecollecurl)

{

  try
     {
        SPSecurity.RunWithElevatedPrivileges(delegate()
          {
         using (SPSite _CurrentSelectedSite = new SPSite(strcurrentSelectedURLSiteNameList))
              {
                  using (SPWeb _CurrentSelectedWeb = _CurrentSelectedSite.OpenWeb())
                  {
                      _CurrentSelectedWeb.AllowUnsafeUpdates = true;
                      string mmcurrSelURL = string.Empty;
                
                  if (_CurrentSelectedWeb.Webs.Count > 0)
                   {
                      SPWebCollection childWebsofSiteCollec = _CurrentSelectedWeb.Webs;

                     GetListofWebs(childWebsofSiteCollec);

  }

 

 private void GetListofWebs(SPWebCollection childWebsofSiteCollect)
    {
        foreach (SPWeb singleSelectedWeb in childWebsofSiteCollect)
        {
            _mCurrSubWebTitleRetrieve = singleSelectedWeb.Title.ToString(); // (paramcurrSelectedNodeNameforSubSite)))
            _mCurrSubWebTitleURLRetrieve = singleSelectedWeb.Url.ToString();
            if (_mCurrSubWebTitleRetrieve != “Search”)
            {                                             

                try
                {
                    dttableGrpNamenAccessRights = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPerms)
                {
                    LogthisError(expinRetrievenApplyPerms);
                }
                //NOW APPLY THE PERMISIONS
                try
                {
                   
                    dataTableGrpNamenAccessRightsContentType = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPermsforCType)
                {
                    LogthisError(expinRetrievenApplyPermsforCType);
                }
            }
            try
            {
                GetListofWebs(singleSelectedWeb.Webs);
            }
            catch (Exception expinGetlistofwebs)
            {
                LogthisError(expinGetlistofwebs);
            }
        }
    }

}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s