Showing posts with label SPQuery. Show all posts
Showing posts with label SPQuery. Show all posts

SP Web Application Get Central Administration

With this code you can get the central administration webapplication.


   1:  private static SPWebApplication GetCentralAdministrationWebApplication()
   2:  {
   3:      SPWebService cS = SPWebService.ContentService;
   4:      var service = cS.Farm.Services.GetValue<SPWebService>("WSS_Administration");
   5:      SPWebApplicationCollection webApplications = service.WebApplications;
   6:      foreach (SPWebApplication webApplication in webApplications)
   7:      {
   8:          return webApplication;
   9:      }
  10:      return null;
  11:  }


If you have better solution, just tell me !

Collection Of using SPSiteDataQuery in whole site

To set up a query across a whole site collection you can use the SPSiteDataQuery object.

The following method fetches all .doc files from all doclibs of the site collection and prints out a list of urls to those items.


public void TestSiteDataQuery()
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb("/"))
{
SPSiteDataQuery query = new SPSiteDataQuery();

// Search in doclibs only
query.Lists = "<Lists BaseType='1' />";

// Only .doc files
query.Query =
@"<Where>
<Eq>
<FieldRef Name='DocIcon' />
<Value Type='Computed'>doc</Value>
</Eq>
</Where>"
;

// Select only needed columns: file reference
query.ViewFields = "<FieldRef Name='FileRef' />";

// Search in all webs of the site collection
query.Webs = "<Webs Scope='SiteCollection' />";

// Perform the query
DataTable table = web.GetSiteData(query);

// Generate an absolute url for each document
foreach (DataRow row in table.Rows)
{
string relativeUrl =
row["FileRef"].ToString().Substring(
row["FileRef"].ToString().IndexOf("#") + 1);
string fullUrl = site.MakeFullUrl(relativeUrl);

// Write urls to console
Console.WriteLine(fullUrl);
}
}
}
}

If you have better solution, just tell me !