Sunday, May 12, 2013

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? 

C#: 
SPSite oSPSite = new SPSite("http://mysite");
SPWeb oSPWeb = oSPSite.OpenWeb(); 

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?  

 using(SPSite mySite = new SPSite("yourserver"))
{
 using(SPWeb myWeb = mySite.OpenWeb())
 {
   SPList interviewList = myWeb.Lists["listName"];
   SPListItem newItem = interviewList.Items.Add();
   newItem["interview"] = "interview";
   newItem.Update();
 }


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? 

SPList interviewList = myWeb.Lists["listName"];
 foreach (SPListItem interview in interviewList)
{
      //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 CAML Query to get values from list. This method is faster compared to above

SPList interviewList = myWeb.Lists["listName"];
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.

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.

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 you 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.


Reference:
http://mosshelper.weebly.com/sharepoint-interview-questions-on-sharepoint-object-model.html


No comments:

Post a Comment

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...