Showing posts with label sharepoint. Show all posts
Showing posts with label sharepoint. Show all posts

Thursday, July 2, 2015

How to sort SharePoint object of SPWeb in SPWebCollection based on Title?

A questioner posed an interesting question on SharePoint StackExchange – how to get all the web sites (SPWeb) in SharePoint and sort them alphabetically.

SPSite.AllWebs returns a collection of all web sites that are contained within the site collection. But they seem to be in random order and are not sorted alphabetically based on their Title. Sorting these web sites based on their title is actually pretty easy.

SPSite.AllWebs returns object of SPWebCollection which inherits from IEnumerable<T> interface. This interface has a function called OrderBy using which we can order the SPWeb based on its Title property.

SPWebCollection webColl = site.AllWebs;
// Sort the SPWeb alphabetically based on Title
IEnumerable sortWebColl = webColl.OrderBy(w => w.Title); 
foreach (SPWeb web in sortWebColl) {
    // Code
}

You also need to make sure that you use the following namespaces otherwise it will throw error.

using System.Linq;
using System.Collections;
using System.Collections.Generic;

You can extend the above code snippet to sort based on Description and other properties of SPWeb also.

Wednesday, May 20, 2015

Get appWebUrl and hostUrl in SharePoint Online / Office 365

When you are developing in SharePoint Online / Office 365 you will have to fetch appWebUrl and hostUrl if you want to perform any operation on your lists and libraries. I could find two ways of getting these values.

1. Reading values of SPAppWebUrl and SPHostUrl from query string


When you run your project it opens up the page Default.aspx. The URL of this page looks something like this.


Notice the query string parameters of SPAppWebUrl and SPHostUrl. You can fetch these values via a helper function.

Friday, April 24, 2015

Permission, Permission Level and Permission Policy in SharePoint

Permission, Permission Level and Permission Policy are three very different functionalities yet closely related in SharePoint. This article presents my understanding of what they are and how they work in SharePoint. It has been written with SharePoint 2010 in reference.

Permission


Permission means an action allowing someone to do a particular thing. In SharePoint it means the same. So you have permissions like Add Items, Edit Items, Delete Items and 30 more permissions. Yes, there are in all 33 permissions in SharePoint.

Permissions are divided into three categories.

  • Site Permissions (18) – apply generally across a SharePoint site
  • List Permissions (12) – apply to content in lists and libraries
  • Personal Permissions (3) – apply to content that belongs to a single user

Permissions also depend on other permissions. For e.g. you must be able to open an item to view it. In this way, View Items permission depends on Open permission. Below is the complete list of all the permissions and dependent permissions.

Thursday, February 5, 2015

Significance of BaseViewID="0" in schema.xml for SharePoint List Definition

Whenever you create a SharePoint List Definition in Visual Studio the schema.xml file contains two entries for View.

<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">

<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" 
      DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" 
      DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" 
      SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" 
      Url="AllItems.aspx">

Note that one of the views has BaseViewID="0" while other one has BaseViewID="1". The view with BaseViewID set to 1 is the default view with Url set to AllItems.aspx. But the view with BaseViewID set to 0 has no Url attribute set. Any modification for BaseViewID="1" gets reflected on the default view but I couldn’t figure out where the BaseViewID="0" view was being used. Even MSDN documentation is not very clear about it. A bit puzzled about this I posted this question on SharePoint StackExchange and was pointed to this answer which explains the reason for this additional view.

Wednesday, February 4, 2015

Prevent adding duplicates records from predefined data in ListInstance while reactivating its feature

In SharePoint if we have predefined data in ListInstance then deactivating and then reactivating its feature causes the predefined data to created again resulting in duplicates. Say, for e.g. our ListInstance looks something like this.

<ListInstance ...>
  <Data>
    <Rows>
      <Row>
        <Field Name="Title">Record 1</Field>
      </Row>
      <Row>
        <Field Name="Title">Record 2</Field>
      </Row>
    </Rows>
  </Data>
</ListInstance>

If we deploy this to a SharePoint site and if we deactivate and reactivate its feature then we will have “Record 1” and “Record 2” appear twice. Deactivating and reactivating it again would only create more duplicates.

Tuesday, July 22, 2014

Using ‘html’ option in SP.UI.ModalDialog.showModalDialog

I was trying to use SP.UI.ModalDialog.showModalDialog with html option using the below code:


But I was running into this weird error:

Friday, December 20, 2013

View all the event receivers for a SharePoint list from Windows PowerShell ISE

Trying out event receivers in SharePoint I wanted to see which event receivers I have attached to my list. From here and here I was finally able to get a small script which when run in Windows PowerShell ISE would give you just that.


The Add-PSSnapin command adds the SharePoint snap-in to current session so that you can use the commands like Get-SPSite. If you don't add that then you would end up with the error "The term 'Get-SPSite' is not recognized as the name of a cmdlet, function…". Also once you run the command of Add-PSSnapin then you need not run it again and again for current session.

Friday, November 22, 2013

Creating Web Part Page in SharePoint 2013

Creating a Web Part Page required you to follow just two simple steps in SharePoint 2010.

1. Go to Sites actions > More Options.


2. Select Web Part Page.


That’s it you are done. But SharePoint 2013 complicates this a little. So here’s what need to do if want to create a Web Part Page in SharePoint 2013.