I am trying to get site's URL here but not able to figure out how to get it,
using (var mgr = new ServerManager())
{
foreach (var site in mgr.Sites)
{
var siteURL = site. ??
Here's class I am using
http://msdn.microsoft.com/en-us/library/microsoft.web.administration.application.virtualdirectories(v=vs.90).aspx
Umm..I guess this might help..
ServerManager serverMgr = new ServerManager();
Site site = serverMgr.Sites["YourSiteName"];
List<string[]> urls = new List<string[]>();
foreach (Binding binding in site.Bindings)
{
string bindingInfo = binding.BindingInformation;
string subString = bindingInfo.Substring(2, bindingInfo.Length - 2);
string[] adrs = subString.Split(':');
adrs[0] = "localhost:" + adrs[0];
urls.Add(adrs);
}
Sites is a Sitecollection and you can get sites by looping through its items.
If you are just looking for website's url why dont you use Request object.
Related
I'm having a web api which is having some data in its body and that data i want to add to SharePoint online list using c#. But below code is giving me unauth error.
using (var context = new ClientContext(siteUrl))
{
context.ExecutingWebRequest += Context_ExecutingWebRequest; // for oAuth it working in get list dat
Web web = context.Web;
List topicsList = context.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation newTopicInfo = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(newTopicInfo);
oListItem["Title"] = "Test";
oListItem["Column1"] = "Test1";
oListItem.Update();
context.ExecuteQuery();
}
The above code is working perfectly fine now, i was missing permissions to my app. For using oAuth we have to register an Add-in in SharePoint, and to post data in sharepoint i have to give FullControl to my add-in, but while creation i have made it read-only.
Below is the referance.
Referance
For SharePoint Online, use SharePointOnlinCredentials class pass credentials to authencation:
string password = "*******";
string account = "username#tenant.onmicrosoft.com";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
using (ClientContext ctx = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
{
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List topicsList = ctx.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation oListItemCreationInformation = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(oListItemCreationInformation);
oListItem ["Title"] = "New List Item";
oListItem["Column1"] = "Test1";
oListItem .Update();
ctx.ExecuteQuery();
};
Reading userprofile data from Sharepoint online is straightforward with CSOM. For example:
string siteURL = #"https://foo.sharepoint.com";
ClientContext ctx = new ClientContext(siteURL);
var securePass = new SecureString();
"password123".ToList().ForEach(s => securePass.AppendChar(s));
ctx.Credentials = new SharePointOnlineCredentials("userName", securePass);
var peopleManager = new PeopleManager(ctx);
var personProperties = peopleManager.GetMyProperties();
ctx.Load(personProperties, p => p.UserProfileProperties);
ctx.ExecuteQuery();
Is this possible to do to a Sharepoint 2013 on premise using WebClient or something similar?
I need to remove groups from a SharePoint site that contain an underscore in the name. I need something like the below code, but I am unable to use .Contains on the collGroups.
Any idea how I can do this?
using (SPSite oSite = new SPSite(spsite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPGroupCollection collGroups = oWeb.SiteGroups;
if(collGroups.Contains("_")) //this doens't work, but I need something like this
{
group.Delete();
}
}
}
For future reference: here is what I ended up writing, thanks to gunr2171 for pointing me in the right direction!
using (SPSite oSite = new SPSite(spsite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
var result = (from g in oWeb.Groups.OfType<SPGroup>()
where g.Name.Contains("_")
select g).ToList();
foreach (SPGroup group in result)
{
SPGroupCollection collGroups = oWeb.SiteGroups;
collGroups.Remove(group.Name);
Console.WriteLine("Removed " + group.Name);
}
Console.WriteLine("Process Complete!");
}
How to get fields values from a particular list item.In my case i want to get all form fileds of Workplan list.Actually i want to get Workplan all list item and insert to sharepoint 2013 associated database.
I try the following code.
string strUrl = "http://example.com/default.aspx";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[52];
SPQuery myquery = new SPQuery();
myquery.Query = "";
SPListItemCollection items = list.GetItems(myquery);
foreach (SPListItem item in items)
{
if (item != null)
{
var Name = item.ListItems.Fields.List;
Console.WriteLine("Name is :" + Name);
}
}
}
}
This is the easiest way I can think of using Server Object Model:
string strUrl = "http://example.com";
using(SPSite oSite = new SPSite(strUrl))
{
using(SPWeb oWeb = oSite.OpenWeb())
{
SPList list = oWeb.Lists["Workplan"];
foreach(SPField field in list.Fields)
{
Console.WriteLine(field.Title);
}
}
}
Btw, as for your site-URL "http://example.com/default.aspx" it is enough to do it like "http://example.com".
For more information on Sharepoint I recommend using this site in the future.
using (SPSite site = new SPSite("URL")
{
using (SPWeb web = site.OpenWeb("sitecollection/subsite"))
{
//to get specific list type
string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
SPList list = web.GetList(listUrl);
Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
}
}
//To get all lists from spweb use this:
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebs = oSiteCollection.AllWebs)
{
foreach (SPWeb oWebsite in collWebs)
{
SPListCollection collSiteLists = oWebsite.Lists;
foreach (SPList oList in collSiteLists)
{
//get your each list here
}
oWebsite.Dispose();
}
}
i have a list in SharePoint 2010 that has fields "title,count" .one of the fields (count) possible to change.
i want to access the last version of this property. by this article: but there are no accepted answer yet.
File file;
FileVersionCollection versions;
ClientContext clientContext;
IEnumerable<Microsoft.SharePoint.Client.FileVersion> oldVersions;
clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new NetworkCredential(user, password, shp.domainname);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
string path = web.ServerRelativeUrl + "/Lists/" + listname + "/" + id1 + "_.000"; // it represet list id 7
file = web.GetFileByServerRelativeUrl(path);
ListItem versionListItem = file.ListItemAllFields;
clientContext.Load(versionListItem);
clientContext.ExecuteQuery();
versions = file.Versions;
clientContext.Load(versions);
oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.ExecuteQuery();
if (oldVersions != null)
{
foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
{
DateTime date = new DateTime();
date = _version.Created.Date;
here i can access to properties but "count" doesn't exist here.
i want to have differences by last versions like SharePoint
i have other method (find it here ) that return for me those last property values
ClientContext clientContext = new ClientContext("http://basesmc2008");
Web site = clientContext.Web;
clientContext.Load(site);
clientContext.ExecuteQuery();
File file = site.GetFileByServerRelativeUrl("/Shared Documents/test.tif");
clientContext.Load(file);
clientContext.ExecuteQuery();
ListItem currentItem = file.ListItemAllFields;
clientContext.Load(currentItem);
clientContext.ExecuteQuery();
FileVersionCollection versions = file.Versions;
IEnumerable<FileVersion> oldVersions = clientContext.LoadQuery(versions.Where(v => v.VersionLabel == "1.0"));
clientContext.ExecuteQuery();
if (oldVersions != null)
{
foreach (FileVersion oldFileVersion in oldVersions)
{
File oldFile = site.GetFileByServerRelativeUrl("/" + oldFileVersion.Url);
clientContext.Load(oldFile);
clientContext.ExecuteQuery();
ListItem oldItem = oldFile.ListItemAllFields;
clientContext.Load(oldItem);
clientContext.ExecuteQuery();
//error here
}
error here
An error has occurred.","ExceptionMessage":"Value does not fall within
the expected range
is there any solution shorter than this for retrieve all last values of specific field?
Sadly actual version data is not available from CSOM. You can get the data by extracting it from /_layouts/Versions.aspx?list={[THE LIST ID]}&ID=[THE ITEMID]&IsDlg=1;
I used HTMLAgilityPack to parse out the data I was looking for (a past value for a specific field)
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(DownloadedPageContent);
HtmlNodeCollection n = doc.DocumentNode.SelectNodes("//tr[#id='vv" + VersionId + FieldName +"']/td");
String value = n[1].InnerText.Trim();