Pages

Tuesday 7 April 2015

Lists

What are Sharepoint Lists?

SharePoint lists are tables of data, much like Excel spreadsheets. But lists can do a lot more than just store columns and rows of data—in fact, lists are like mini-applications in SharePoint.   
Create a list using one of the built-in templates. 
Add columns to collect additional data and calculate values as required. 
Create supporting lists for lookups and master lists. 
Add views to display required reports. 
Enable email, versioning, and item approval as required. 
Optionally customize the data entry forms. 
Save the final list as a template and deploy it to other site collections.

As a developer you sometimes forget to look at the functionality that is available just out of the box. One of those things is the standard functionality that is available in the SharePoint lists. Most people knowledgable of SharePoint will think: what is your point? Didn't you know this? Why do you bother me with this: read the f*cking manual. For those other people who forgot to read the manual, I would like to enumerate some things about lists that are "out of the ordinary".

Did you know that:

Lists based on the "Links" list have a "Change Order" option in the menu bar of it's views that allow you to reorder the list items 
Lists based on "Tasks" and "Issues" have an "Assign to:" field that is a lookup to users in the UserInfo list for the web the list is on. This list contains all users that ever visited the list. What does this mean:

Users who did not visit the web yet are not in the list 
Users who ever visited the web site, but whom's account is now removed is still in the list (Should be like this: task/issue can be assigned to user that is now gone, if you edit the task/issue to change something, the user shouls still be in the list, otherwise it must always be reassigned on edit.) 
System accounts that crawl the list are in the list (we filter those out using the MacawSharePointSkinner)
Lists based on "Issues" have an option to sent the "Assigned To" person a notification e-mail (General Settintgs ->Email Notification) on changed item or when ownership is assigned. 
Lists based on "Issues" have a "View reports" action with reports to track issue trends 
Lists based on "Contacts" have "Export Contact" functionality in the menu of an entry that created a vcard file 
Lists based on "Contacts" can "link to Outlook", this creates a read-only contacts list in outlook, with in each item a link to update the contact. This list in outlook stays up to date automatically 
Lists based on "Contacts" can import contacts from the address book 
Lists based on "Events" can "link to Outlook", this creates a read-only calendar in outlook, with in each item a link to update the event. This list in outlook stays up to date automatically 
Lists based on "Discussion" support threading on items, and that this list sucks for discussions (in my opinion). We rewrote the new/edit/view pages to se things like the thread you are replying on, the complete thread of a single discussion item, see the newest discussion thread first. This makes it usable. 
List items can have multiple files attached 
There are no events available on lists, only on libraries. When I asked Mike Fitzmaurice about this on the PDC 2003 he answered that it was due to lack of time. There was a big launching customer who needed events on libraries, so this was implemented.
All this functionality is somewhere available in the list definitions, so if you define your own template you can combine all this functionality!!

Server Object Model



Sharepoint Interview Questions on Sharepoint Object Model

What is a SPSite and SPWeb object, and what is the difference between each of the objects?  

The SPSite object represents a collection of sites (site collection [a top level sites and all its subsites]). The SPWeb object represents an instance SharePoint Web, and SPWeb object contains things like the actual content. A SPSite object contains the various subsites and the information regarding them. 
 
How would you go about getting a reference to a site? 

Select For Unformatted Code
C#: 
oSPSite = new SPSite("http:/server");
oSPWeb = oSPSite.OpenWeb(); 
Internet Explorer 6, Netscape Navigator 6.2 or later.

What does a SPWebApplication object represent?  

The SPWebApplication objects represents a SharePoint Web Application, which essentially is an IIS virtual server. Using the class you can instigate high level operations, such as getting all the features of an entire Web Application instance, or doing high level creation operations like creating new Web Applications through code. 
 
Would you use SPWebApplication to get information like the SMTP address of the SharePoint site? 
Yes, since this is a Web Application level setting. You would iterate through each SPWebApplication in the SPWebApplication collection, and then use the appropriate property calls (OutboundMailServiceInstance) in order to return settings regarding the mail service such as the SMTP address. 

How do you connect (reference) to a SharePoint list, and how do you insert a new List Item?  

Select For Unformatted Code
C#: 
1. using(SPSite mySite = new SPSite("yourserver"))
2. {
3. using(SPWeb myWeb = mySite.OpenWeb())
4. {
5. SPList interviewList = myWeb.Lists["listtoinsert"];
6. SPListItem newItem = interviewList.Items.Add();
7. 8. newItem["interview"] = "interview";
9. newItem.Update();
10. }
11. } 

How would you loop using SPList through all SharePont List items, assuming you know the name (in a string value) of the list you want to iterate through, and already have all the site code written? 

Select For Unformatted Code
C#: 
1. SPList interviewList = myWeb.Lists["listtoiterate"];
2. foreach (SPListItem interview in interviewList)
3. {
4.  Here you put any condition to filter the values
            If(Interview[“name”].ToString()==”test”)
{
Add to ArrayList
}

“name” is column name in particular list
}

Another method use can CAML Query to get values from list. This method is faster compared to above

SPList interviewList = web.Lists["listtoiterate"];
SPQuery spQuery = new SPQuery();
spQuery.Query = @”<Where>
  <Eq>
    <FieldRef Name="name" />
      <Value Type="Text">test</Value>
  </Eq>
</Where>”;

SPListItemCollection queryitems = interviewList.GetItems(spQuery);

What does AllowUnsafeUpdates do ?

If your code modifies Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, without requiring a security validation. You can do by setting the AllowUnsafeUpdates property.

C#:
using(SPSite mySite = new SPSite("yourserver"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
myWeb.AllowUnsafeUpdates = true;
SPList interviewList = myWeb.Lists["listtoinsert"];
SPListItem newItem = interviewList.Items.Add();

newItem["interview"] = "interview";
newItem.Update();
}
}

What does RunWithElevatedPrivileges do?

Assume that you have a Web Part in which you want to display information obtained through the Windows SharePoint Services object model, such as the name of the current site collection owner, usage statistics, or auditing information. These are examples of calls into the object model that require site-administration privileges. Your Web Part experiences an access-denied error if it attempts to obtain this information when the current user is not a site administrator. The request is initiated by a nonprivileged user. you can still successfully make these calls into the object model by calling the RunWithElevatedPrivileges method provided by the SPSecurity class.
C#:
SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate() {

using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID)) {
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID)) {
string SiteCollectionOwner = ElevatedsiteColl.Owner.Name;
string Visits = ElevatedsiteColl.Usage.Visits.ToString();
string RootAuditEntries =
ElevatedSite.RootFolder.Audit.GetEntries().Count.ToString();
}
}

});

In some place you will get error “Access Denied”  like when you try to execute caml query or something in that case if ypu place you caml query code inside this RunWithElevatedPrivileges delegate then automatically it will take system account rights and run the code.

What is ServerUpdate() and SystemUpdate() ?

Any changes in the list, i.e. new addition or modification of an item.. the operation is complete by calling the Update method.


But if a List is set to maintain versions .. and you are editing an item, but don't want to save it as a new version, then use the SystemUpdate method instead and pass in 'false' as the parameter.