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();
};
Related
I am going to initialize items of a sharepoint list. One of field is a lookup one, but when I'm going to initialize it, no value is set to it.
Here is my code:
var clientContext =
new ClientContext(aURL)
{
Credentials = new System.Net.NetworkCredential(somestring)
};
Web oWebsite = clientContext.Web;
List teachersList = oWebsite.Lists.GetByTitle("Teachers");
FieldLookupValue lookupField = new FieldLookupValue();
lookupField.LookupId = anInteger;
teacherInfoListItem["ProfessorID"] = lookupField;
teacherInfoListItem["Title"] = value;
teacherInfoListItem["LastName"] = value;
teacherInfoListItem.Update();
clientContext.ExecuteQuery();
Your code logic should be fine, make sure the anInteger item exists in your lookup list.
My tested code.
using(var clientContext =new ClientContext("http://sp"))
{
var web = clientContext .Web;
var oList = web.Lists.GetByTitle("TestDetails");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
FieldLookupValue lookupField = new FieldLookupValue();
lookupField.LookupId = 1;
oListItem["Title"] = "My New Item!";
oListItem["Name"] = lookupField;
oListItem.Update();
clientContext.ExecuteQuery();
Console.WriteLine("complete");
}
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 have a little problem with my Sharepoint 2 Exchange Tool. Maybe sb from you can help me here :)
using (ClientContext clientContext = new ClientContext(m_Office365URL))
{
SecureString passWord = new SecureString();
foreach (char c in m_password.ToCharArray()) passWord.AppendChar(c);
SharePointOnlineCredentials xCred = new SharePointOnlineCredentials(m_userName, passWord);
clientContext.Credentials = xCred;
Web xWeb = clientContext.Web;
clientContext.Load(xWeb);
clientContext.Load(clientContext.Site.RootWeb);
clientContext.ExecuteQuery();
DateTime calDate = startDate;
List targetList = xWeb.Lists.GetByTitle(m_TargetListName);
clientContext.Load(targetList);
clientContext.ExecuteQuery();
This is fully working an haven't any error inside.
Now my thing is to get this as similar as possible working for Exchange.
Here's what I have until now:
ExchangeService m_Service = new ExchangeService(m_Url);
SecureString passWord = new SecureString();
foreach (char c in m_Password.ToCharArray()) passWord.AppendChar(c);
m_Service.Credentials = new WebCredentials(m_UserName, m_Password);
foreach (Appointment appointment in m_Service.FindItems(WellKnownFolderName.Calendar, new ItemView(int.MaxValue)))
{
DateTime calDate = startDate;
ExchangeService xWeb = m_Service;
m_Service.UpdateItems(xWeb);
m_Service.Load(m_Service.Site.RootWeb);
m_Service.ExecuteQuery();
List targetList = xWeb.Lists.GetByTitle(m_TargetListName);
m_Service.Load(targetList);
m_Service.ExecuteQuery();
But As you can guess I get Poblems startin at "ExchangeService xWeb = new m_Service"
For sure the m_Service.UpdateItems, m_Service.Load etc won't work either.
If somebody might have a hint for me that'd be great.
I was able to run this code using:
System.Net.ServicePointManager.ServerCertificateValidationCallback = Ise_ExchangeInterface.CertificateValidationCallBack;
m_Service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
m_Service.Credentials = new WebCredentials(m_UserName, m_Password);
m_Service.AutodiscoverUrl(m_UserName, Ise_ExchangeInterface.RedirectionUrlValidationCallback);
A SharePoint list is being created using Sharepoint.client but the template is not getting applied. I don't see any fields/columns in the new list. A similar questions was asked here but it's answer did not work for me. Any help is greatly appreciated!
using (ClientContext context = new ClientContext(SITE))
{
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
ListTemplate template = null;
ListTemplateCollection ltc = context.Site.GetCustomListTemplates(web);
//ListTemplateCollection ltc2 = web.ListTemplates;
context.Load(ltc);
context.ExecuteQuery();
foreach (ListTemplate t in ltc)
{
if (t.Name == "My_Template")
{
template = t;
break;
}
}
var listCreationInfo = new ListCreationInformation
{
Title = "Test_List",
Description = "Test"
};
//ListTemplate listTemplate = ltc.First(listTemp => listTemp.Name.Contains("My_Template"));
listCreationInfo.TemplateFeatureId = template.FeatureId;
listCreationInfo.TemplateType = template.ListTemplateTypeKind;
listCreationInfo.QuickLaunchOption = QuickLaunchOptions.On;
List oList = web.Lists.Add(listCreationInfo);
context.ExecuteQuery();
}
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.