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
Related
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);
}
}
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).
What is the best way to get the SPSite in the execute method of a Sharepoint JobDefinition?
I am using the next code:
SPServer ms = SPServer.Local;
SPSite msite = new SPSite(server);
But it does not works when implemented in a Sharepoint Farm ... and if I try
SPSite msite = SPContext.Current.Site;
it does not work because the SPContext does not exist in the execute of the Job...
Any ideas?.
Thanks...!!!
SPFarm farm = SPFarm.Local;
SPWebService webService = farm.Services.GetValue<SPWebService>("");
SPWebApplication webApplication = webService.WebApplications["Web Application name"];
foreach (SPSite site in webApplication.Sites)
{
// ...
}
For Web Application-Scoped timer jobs you can also use the SPJobDefinition.Parent property and cast it to the SPWebApplication type.
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!");
}
I have a document which is stored in doc library of sharepoint..now i want to open and read data from it ...how can i do it ..filestream does not take url as input ..please help.
Try SPFile.OpenBinaryStream
From SharePoint 2007 - Read content from SPFile:
string content = string.Empty;
using (SPSite oSite = new SPSite("http://localhost/"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPDocumentLibrary doclib = (SPDocumentLibrary)oWeb.GetList(DocLibUrl);
SPFile htmlFile = doclib.Items[0].File;
using (System.IO.StreamReader reader = new System.IO.StreamReader(htmlFile.OpenBinaryStream()))
{
content = reader.ReadToEnd();
}
}
}
Sounds like you need to use a HTTPRequest object to retrieve the file. Here is a code example:
http://geeknotes.wordpress.com/2008/01/10/saving-a-possibly-binary-file-from-a-url-in-c/