Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. 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 !

Lookup fields and event lists - JQuery and SharePoint

JQuery to change the width of a mulitvalued lookup field in SharePoint and to hide the Workspace field in an event list.

The script to change the width of multivalued lookup fields:



<script type="text/javascript">
$(document).ready(function()
{
$("select[id*='<INSERT_YOUR_FIELDNAME>']").parent().width(300);
$("select[id*='<INSERT_YOUR_FIELDNAME>']").parent().height(200);
$("select[id*='<INSERT_YOUR_FIELDNAME>']").width(300);
});
</script>

The script to hide the workspace checkbox in an event list:

This first bit of script hides the ‘Workspace’checkbox in the NewForm and the EditForm. The script to hide the workspace field from the DispForm.aspx:


<script type="text/javascript">
$(document).ready(function()
{
$("a[name='SPBookmark_WorkspaceLink']").parent().parent().parent().hide();
});
</script>


<script type="text/javascript">
$(document).ready(function()
{
$("a[name='SPBookmark_WorkspaceLink']").parent().parent().parent().hide();
});
</script>

There are several ways to add the script to the pages. For the Lookup fields I used the I descibe in this blog post. For the Event list, I created custom EditForm, NewForm and DispForm pages and added the script to those pages directly.


If you have better solution, just tell me !

How To Change the Default EditForm, NewForm and DispForm

Problem

You’ve developed some custom new, edit and display application pages that are stored in the _layouts directory. Let’s assume the filenames are newform.aspx, dispform.aspx and editform.aspx. You now want to change the properties of your custom list so that any new, edit or display requests point to your custom pages.
If you open up your site within SharePoint Designer and expand the Lists folder, from there you can access the properties of that custom list by right clicking on it and selecting Properties. Once the List Properties pane is open, click on the Supporting Files tab.



You’ll see there that you can actually choose what display, edit and new forms you want your list to be using. So if you’ve developed something custom, you would click Browse…, point to your new location and select the file. Here is where it starts to fail, I am only able to browse within the site itself and cannot navigate to my _layouts directory to select the files I mentioned above.

Solution

I suspected this was a limitation of the SharePoint Designer UI and not actually a limitation of the Object Model. I did a bit of fiddling with my SPList object and was not able to find anything that let me change those properties. If you look at the screenshot above, there is a key piece of information that’ll make the light turn on (at least it did for me). The Supporting Files tab has a drop down selector for the Content type specific forms. If you think about that a moment, you’ll remember that ALL lists within SharePoint inherit from a default content type.


So armed with that knowledge, I created an SPContentType object and took a look at it’s properties and methods. Sure enough, there are 3 properties I can set to change these forms: EditFormUrl, NewFormUrl and DisplayFormUrl. Here is some sample code I used to change the forms for my custom list called ‘My List’ that inherits from the Item content type:

So in the example code above, I had to determine what default content type my list inherited from. In this case it was the Item content type.
So in the example code above, I had to determine what default content type my list inherited from. In this case it was the Item content type.

NOTE:

If you need to change the content type directly, you can definitely do that, however, I did a bit of testing and found that any lists ALREADY inheriting from that content type did not pickup my changes to the form locations. Any NEW list that I created that was inheriting from that content type did pickup the changes. In order for me to change the form locations for the existing list I had to use the code above. For reference, here is how you would change the content type directly. The code has only a subtle difference.

1
2
3
4
5
6
7
8
9
10
11
SPWeb web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

SPContentType ct = web.ContentTypes["Name Of Your Content Type"];

ct.EditFormUrl = "_layouts/editform.aspx";
ct.NewFormUrl = "_layouts/newform.aspx";
ct.DisplayFormUrl = "_layouts/dispform.aspx";

ct.Update();

As always, any questions, let me know!

How to Write An SPQuery Using Multiple AND OR Operators

Ok, if you’re like me, writing CAML queries can sometimes make your head hurt. Not because it’s terribly complicated, but because there’s not much useful information out there that demonstrates, with REAL examples, how these should be written. So let’s see if we can clarify this:

Scenario 1

Get me all items in a list WHERE fullName equals the currently logged in user.

1
2
3
4
5
6
7
8
9
10
11
SPWeb web = SPControl.GetContextWeb(Context);

string fullName = web.CurrentUser.Name;

SPQuery oQuery = new SPQuery();

oQuery.Query =
"<Where>" +
"<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
"</Where>" +
"<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 2

Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’.

1
2
3
4
5
6
7
8
9
10
11
12
13
SPWeb web = SPControl.GetContextWeb(Context);

string fullName = web.CurrentUser.Name;

SPQuery oQuery = new SPQuery();
oQuery.Query =
"<Where>" +
"<And>" +
"<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
"<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
"</And>" +
"</Where>" +
"<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 3

Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’ AND manager is James Lane.

This is where it gets a bit tricky. The following example is INCORRECT and will produce an error when run:

This is the correct way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SPWeb web = SPControl.GetContextWeb(Context);

string fullName = web.CurrentUser.Name;

SPQuery oQuery = new SPQuery();
oQuery.Query =
"<Where>" +
"<And>" +
"<And>" +
"<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
"<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
"</And>" +
"<Eq><FieldRef Name='Manager'/><Value Type='Text'>James Lane</Value></Eq>" +
"</And>" +
"</Where>" +
"<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 4

Get me all items WHERE fullName equals the currently logged in user AND status equals ‘Complete’ OR status equals ‘On Hold’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SPWeb web = SPControl.GetContextWeb(Context);

string fullName = web.CurrentUser.Name;

SPQuery oQuery = new SPQuery();
oQuery.Query =
"<Where>" +
"<And>" +
"<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
"<Or>" +
"<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
"<Eq><FieldRef Name='Status'/><Value Type='Text'>On Hold</Value></Eq>" +
"</Or>" +
"</And>" +
"</Where>" +
"<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

I really do recommend if you’re doing any work with CAML queries that you download the U2U CAML Query Builder 2007. It doesn’t come without it’s issues, for example, doing a query on a list with a column of type Lookup does not always yield the results I would expect, but it’s still quite helpful. I also often find myself changing the Value Type from Lookup to Text but other than that it’s a huge resource when trying to determine if the data is actually in the list or if there is a problem with the query i’ve written.

Any feedback or questions, please drop me a line.

If you have better solution, just tell me !