Get List Item by GUID - sharepoint-2010

I am working on sharepoint-2010 and I am new to sharepoint.
I want to access List Item by Guid. So, how can I access.
Please provide some reference or some class file to access that.
Thanks in advance.

Use the SPList.GetItemByUniqueId method:
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[listTitle];
SPListItem item = list.GetItemByUniqueId(itemGuid);
}
}

Related

Sharepoint 2010 add a choice field

I have to add a choice field (dropdown) to a sharepoint list. I'm using the SharePoint API in Visual Studio.
Here is me code. I have tried to run this and there are no errors but it doesn't work.
private void addChoiceField(SPFeatureReceiverProperties properties, String _listName, String _fieldName)
{
using (SPWeb _web = properties.Feature.Parent as SPWeb)
{
SPList _list = _web.Lists.TryGetList(_listName);
writeLog(properties, "list name:" + _listName);
SPFieldChoice _fieldDD = (SPFieldChoice)_list.Fields[_fieldName];
writeLog(properties, "fieldname:" + _fieldName);
if (_fieldName == "State")
{
_fieldDD.Choices.Clear();
_fieldDD.Choices.Add("Gesloten-Verloren");
_fieldDD.Choices.Add("Analyse nodig");
_fieldDD.Choices.Add("Onderhandeling of revise");
_fieldDD.Choices.Add("Presentatie of demo");
_fieldDD.Choices.Add("Voorstel voor prijsofferte");
_fieldDD.Choices.Add("Prospect");
_fieldDD.Choices.Add("Waardevol");
_fieldDD.Choices.Add("Gesloten-Gewonnen");
_fieldDD.Update();
}
}
}
Does anyone knows what's wrong or how to add a choicefield in a different way using the API?
You need to call
_list.Fields.CreateNewField
to actually add it to the list instead of trying to pull an existing field from the list.
Here is a link to the method description: http://msdn.microsoft.com/en-us/library/microsoft.harepoint.spfieldcollection.createnewfield.aspx

Why is SPWeb.ID empty?

I want to get the GUID for a particular SPWEB.
But when I try to check the value of SPWEB.ID, it is empty.
My code:
**Using blogSiteSPWeb As SPWeb = siteCollectSPSite.OpenWeb(returnedSearchPath(pathCount))**
**For Each subsite As SPWeb In blogSiteSPWeb.Webs
dim guid as GUID
guid = subsite.ID
.......
Next
end using**
This code will work to get the current web context:
string _siteURL = SPContext.Current.Site.Url;
using (SPSite site = new SPSite(_siteURL)
{
{
using (SPWeb web = site.OpenWeb())
... some code here
}
}
Now web.ID will be set to the URL of the current site

Add item to Sharepoint Survey list

I have a survey list in sharepoint and I'd like to add an answer to the survey using c# code, this I think could be interpreted as add an item to the list so I tried the next but is not working.
How can this be done?
using (SPSite site = new SPSite("http://user-pc"))
{
using (SPWeb web = site.RootWeb)
{
try
{
SPList olist = web.Lists["JobSchedulle"]; //<- Name of the survey in sharepoint 2010
SPListItem item = olist.Items.Add();
item[5] = "good";// <- this survey only has one question, and is a text option ..
item.Update();
}
catch {
Console.ReadLine();
}
}
}
I found the error - the code works, but I was accessing item[5] (with the wrong index).

unable to update listitem external data column sharepoint 2010

I try update Extenal data column but it doesn't work, new value is not stored. (new value is visible on details form but not on list, rehreshing external data type does not return related external column values)
using (SPSite oSiteCollection = new SPSite("site.com"))
{
using (SPWeb oWebsite = oSiteCollection.OpenWeb("site.com"))
{
using (SPWeb oWebsiteRoot = oSiteCollection.RootWeb)
{
SPList docLib = oWebsiteRoot.Lists["list name"];
SPListItemCollection items = docLib.Items;
foreach (SPListItem item in items)
{
//item["n"] is external column data field
item["n"] = item["notice"].ToString();
item.UpdateOverwriteVersion();
}
}
}
}
Check using item.Update() insted of item.UpdateOverwriteVersion()
This has to do with the field type and is quite complicated to get right.
There is a free external data field migration/copy tool here:
http://rrfreeman.blogspot.com/2013/06/bcs-bdc-external-data-lookup-field.html
I included source code and links to relevant articles.

Creating a SharePoint 2010 page via the client object model

I am attempting to create pages in a Sharepoint 2010 pages library via the client object model but I cannot find any examples on how to do it. I have tried two approaches:
The first is to treat the Pages library as a list and try to add a list item.
static void createPage(Web w, ClientContext ctx)
{
List pages = w.Lists.GetByTitle("Pages");
//ListItem page = pages.GetItemById(0);
ListItemCreationInformation lici = new ListItemCreationInformation();
ListItem li = pages.AddItem(lici);
li["Title"] = "hello";
li.Update();
ctx.ExecuteQuery();
}
As expected, this failed with the error message:
To add an item to a document library, use SPFileCollection.Add()
The next approach I tried was to add it as a file. The problem is that the FileCreationInformation object is expecting a byte array and I am not sure what to pass to it.
static void createPage(Web w, ClientContext ctx)
{
List pages = w.Lists.GetByTitle("Pages");
FileCreationInformation file = new FileCreationInformation();
file.Url = "testpage.aspx";
file.Content = new byte[0];
file.Overwrite = true;
ctx.Load(pages.RootFolder.Files.Add(file));
ctx.ExecuteQuery();
}
The piece of code above will add an item in the Pages library but opening the file brings up a blank page which I cannot edit. From reading various topics, I suspect that it may only be possible to add pages via server side code. Any thoughts?
Thanks
The problem is that the
FileCreationInformation object is
expecting a byte array and I am not
sure what to pass to it.
You could you whatever method you want to get the page contents into a string (read it from a file, create it using a StringBuilder, etc) and then convert the string to a byte array using
System.Text.Encoding.ASCII.GetBytes()
First of all, Publishing API is not supported via Client Side Object Model (CSOM) in SharePoint 2010. But you could consider the following approach that demonstrates how to create a publishing page using SharePoint 2010 CSOM.
How to create a publishing page using SharePoint 2010 CSOM
public static void CreatePublishingPage(ClientContext ctx, string listTitle, string pageName, string pageContent)
{
const string publishingPageTemplate = "<%# Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%# Reference VirtualPath=\"~TemplatePageUrl\" %> <%# Reference VirtualPath=\"~masterurl/custom.master\" %>";
var pagesList = ctx.Web.Lists.GetByTitle(listTitle);
var fileInfo = new FileCreationInformation
{
Url = pageName,
Content = Encoding.UTF8.GetBytes(publishingPageTemplate),
Overwrite = true
};
var pageFile = pagesList.RootFolder.Files.Add(fileInfo);
var pageItem = pageFile.ListItemAllFields;
if (!ctx.Site.IsPropertyAvailable("ServerRelativeUrl"))
{
ctx.Load(ctx.Site);
ctx.ExecuteQuery();
}
pageItem["PublishingPageLayout"] = string.Format("{0}_catalogs/masterpage/ArticleLeft.aspx, ArticleLeft",ctx.Site.ServerRelativeUrl);
pageItem["PublishingPageContent"] = pageContent;
pageItem.Update();
ctx.ExecuteQuery();
}
Usage
using (var ctx = new ClientContext(url))
{
ctx.Credentials = new NetworkCredential("username", "password", "domain");
CreatePublishingPage(ctx, "Pages", "Greetings.aspx", "Welcome to SharePoint!");
}