Thursday, 26 September 2013

COM/SOM Part-1



Q)How to get page title and URL in SharePoint 2010 using server object model?

To get the Page Title in SharePoint, you can follow the below approach.

User the below namespace:

using Microsoft.SharePoint;

Then write the below code to get the Title

String Title= SPContext.Current.Item["Title"].ToString();

Similarly you can get the URL in the below way

Sting URL=System.Web.HttpContext.Current.Request.Path;


Q)Generate Random password using C#.Net?

public GenerateRandomPassword()

{

String strGuid=System.Guid.NewGuid().ToString();

strGuid=strGuid.Replace("-",string.Empty);

strGuid=strGuid.Substring(0,8); //Here 8 means the password contains 8 characters.

}

Q)To create a list instance with SP object Model.

using System;
using Microsoft.SharePoint;

class Program {
static void Main() {
using (SPSite site = new SPSite("url")) {
using (SPWeb web = site.OpenWeb()) {
string listName = "Litware News";
SPList list = null;
foreach (SPList currentList in web.Lists) {
if (currentList.Title.Equals(listName,
StringComparison.InvariantCultureIgnoreCase)) {
list = currentList;
break;
}
}

if (list == null) {
Guid listID = web.Lists.Add(listName,
"List for big news items",
SPListTemplateType.Announcements);
list = web.Lists[listID];
list.OnQuickLaunch = true;
list.Update();
}
}
}
}
}

Q)How to use GetList() method?


SPList announcementsList = web.GetList("/Lists/Announcements");

Q)To create and save a new list item.

SPListItem newItem = list.Items.Add();
newItem ["Title"] = "Litware Goes Public!";
newItem ["Body"] = " We all live in exciting times.";
newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(2);
newItem.Update();

Q) How to enumerate through the fields in a list by using a foreach construct?

foreach (SPField field in list.Fields)
{
if (!field.Hidden && !field.ReadOnlyField)
Console.WriteLine(field.Title);
}


Q)About SharePoint List?
You can access a List by its name or by its URL.
When retrieving a list by name, you can use the TryGetList method of the SPListCollection, which returns null if the list does not exist.

When retrieving the list by the URL, you can use the GetList method of the SPWeb object, which throws a System.IO.FileNotFoundException if the list does not exist.

Code Samples:
using (SPSite siteCollection = new SPSite("Site URL"))
{
using (SPWeb site = siteCollection.OpenWeb)
{
SPList lstName = site.Lists.TryGetList("NameOfTheList");
if(lstName != null)
//do your work
else
//List does not exists
try
{
SPList taskList = site.GetList("/Lists/Tasks");
//do your work
}
catch(FileNotFoundException)
{
//List does not work
}
}
}

Lists maintain items in a Microsoft.SharePoint.SPListItemCollection object, which is accessible via the Items property of SPList.
Items in the collection are returned as Microsoft.SharePoint.SPListItem objects.
You can iterated over using a foreach statement, or directly accessed using either the item’s ID or index.
Each individual SPListItem maintains a Hashtable of values representing the properties of the item.

//Add new item to list
SPListItem newItem = list.Items.Add;
newItem["Title"] = "This will be the title";
newItem.Update;

//Bind the item to gridview
SPListItemCollection items = list.Items;
gridview1.DataSource = items;
gridview1.DataBind;

//Delete Item by Index
list.Items[0].Delete;

//Update Item by Index
SPListItem updateItem = list.Items[0];
updateItem["Title"] = "Updated Title";
updateItem.Update;

when creating new items in a list, the Update method must be called to save the new item. If the Update method is not called, then the list item will be lost.
if the item was edited by another user before the update is saved, the operation will fail.


Q)HTTP Error 503. The service is unavailable in SharePoint

This error normally comes whenever a user changed the passoword
for the account. So to correct the error follow the below steps:
Go to Start -> All Programs -> Administrative Tools -> Internet
Information Services (IIS) Manager.
Then Click on(+) the server name -> Application Pools then locate
your application pools.
Right click on the Application pool name go to Advance settings ->
Go to process model then Identity Click on the Identity ->Set and
then give the user name, password and confirm password

1 comment: