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?
Related
I try upload file to provided Azure containers which it is taken from ProvisionMigrationContainers() method, however it was not successful.
I am using below C# code:
ClientContext clientContext = new ClientContext(urlSring);
var securePassword = new SecureString();
foreach (var c in passWord) securePassword.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
ClientResult<ProvisionedMigrationContainersInfo> containerInfo = clientContext.Site.ProvisionMigrationContainers();
clientContext.ExecuteQuery();
var containerInfoList = containerInfo.Value;
var dataContainerUri = containerInfoList.DataContainerUri;
var metadataContainerUri = containerInfoList.MetadataContainerUri;
CloudBlobContainer dataContainer = new CloudBlobContainer(new Uri(dataContainerUri));
CloudBlobContainer manifestContainer = new CloudBlobContainer(new Uri(metadataContainerUri));
//Upload data to container start
var testfiles = new[]
{
new SourceFile
{
Filename = "test.txt",
LastModified = DateTime.Now,
Contents = Encoding.UTF8.GetBytes("Hi, this is a test text-file"),
Title = "Title of file 1"
},
new SourceFile
{
Filename = "test2.txt",
LastModified = DateTime.Now.AddDays(-1),
Contents = Encoding.UTF8.GetBytes("Tesfile2"),
Title = "Second title"
}
};
foreach (var testfile in testfiles)
{
var blobReference = dataContainer.GetBlockBlobReference(testfile.Filename);
blobReference.UploadFromByteArray(testfile.Contents, 0, testfile.Contents.Length);
}
When i get list blob from provided Azure containers with below code, it returns null.
var blobList = dataContainer.ListBlobs();
var fileList = blobList.OfType<CloudBlockBlob>().Select(x => x.Name).ToList();
Would you please tell me the reason?
Thanks in advance
RonLee.
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();
};
I am trying to get files results using SearchExecutor.
This us the code:
using (SPSite site = new SPSite("http://SERVER NAME"))
{
using (SPWeb web = site.OpenWeb(""))
{
KeywordQuery keywordQuery = new KeywordQuery(web);
keywordQuery.QueryText = "Author: Moss_User";
keywordQuery.KeywordInclusion = KeywordInclusion.AllKeywords;
keywordQuery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
keywordQuery.TrimDuplicates = false;
keywordQuery.EnableStemming = true;
keywordQuery.EnablePhonetic = true;
keywordQuery.EnableNicknames = false;
keywordQuery.IgnoreAllNoiseQuery = true;
keywordQuery.Timeout = 60000;
keywordQuery.RowLimit = 500;
keywordQuery.SelectProperties.Add("author");
keywordQuery.SelectProperties.Add("SiteName");
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
retunltDataTable = resultTable.Table;
}}
The thing is I am not getting ANY results when searching for:
keywordQuery.QueryText ="Author: Moss_User"
I am getting resukts only when searching for:
keywordQuery.QueryText ="*"
What can be the problem?
Just remove that whitespace after colon
keywordQuery.QueryText ="Author:Moss_User"
Also there is tool for testing queries against Sharepoint search API, it can be useful for testing queries without deploying any code
Sharepoint Search Tool
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);
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.