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.


Comments

Sharepoint essential materials.