Getting Access Denied on sharepoint list - c#

I'm making an ASMX web method call that contains the following code...It ultimately adds a new item to a sharepoint list
string spsite = "http://site/subsite";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite objSite = new SPSite(spsite))
{
using (SPWeb oweb = objSite.OpenWeb())
{
oweb.AllowUnsafeUpdates = true;
SPList list = oweb.Lists["List"];
SPListItem item = list.Items.Add();
item["col1"] = "test";
item["col2"] = "test";
item.Update();
}
}
});
However, I get the following error messages...
You do not have permission to view this directory or page using the credentials that you supplied
Why is this? I thought RunWithElevatedPrivileges does away with this?

Where is this web service hosted? Using RunWithElevtatedPrivileges will use the account that the app pool runs on. If the app pool does not have permission, then you will get access denied.

Related

SharePoint Login Credential

Currently I try to developed application by using SharePoint that retrieve data from List
Now I try to developed for Login Function, The error display as Below
The underlying connection was closed: An unexpected error occurred on a send.
Here with my Source Code
string WebUrl = "https://xxx.sharepoint.com/sites/devspace";
string username = txtUsername.Text;
string pwd = txtPassword.Text;
ClientContext context = new ClientContext(WebUrl);
Web web = context.Web;
SecureString passWord = new SecureString();
foreach (char c in pwd.ToCharArray())
passWord.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(username, passWord);
try
{
context.Load(web);
context.ExecuteQuery();
lblStatus.Text = "Olla " + web.Title;
}catch(Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
Any suggestion for this? Thank in advanced.
Updated:
I'm using SharePoint 2016, login via Windows Server AD.
As discussed, you are using SharePoint 2016, then please download and install CSOM with SharePoint 2016 version Nuget package not SharePoint Online:
Microsoft.SharePoint2016.CSOM
And use this code snippet:
ClientContext clientContext = new ClientContext("https://example.com/sites/testsite/");
clientContext.Credentials = new NetworkCredential("user", "password", "domain");
// Get the SharePoint web
Web web = clientContext.Web;
// Get the SharePoint list collection for the web
ListCollection listColl = web.Lists;
// Retrieve the list collection properties
clientContext.Load(listColl);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Loop through all the list
foreach (List list in listColl)
{
// Display the list title and ID
Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);
}
Console.ReadLine();
SharePoint 2016 On Premise not need to use SharePointOnlineCredentials class, just pass username, password, domain as code snippet above.

get the last modification date in a collection within sharepoint application

I have an asp.net Web Api application which communicates with a sharepoint application via web services.
I add this method to create a list reference with using http request
public static SPService.Lists CreateSPServiceListsReference(HttpRequestMessage request, bool defaultEpic = true)
{
var login = EpicConfiguration.ExtractAuthenticationParameters(request);
var lists = new SPService.Lists(){
Credentials = new NetworkCredential(login.Username, login.Password, login.Domain),
Url = string.Format(SPServiceListFormat, (defaultEpic)?login.EpicWebUrl:login.RefWebUrl)
};
return lists;
}
The this the first time I have to communicate with a sharepoint app. I need to call a service which takes as a parameter the name of the list and returns the last modification date in this this list. I googled before asking this question but I didn't find a solution.
Any ideas?
You could utilize Lists.GetList Method of SharePoint Web Services to retrieve schema for the specified list and then extract Modified property which represents last modified date.
Example
using (var svc = new ListsService.Lists())
{
svc.Credentials = new NetworkCredential(userName, password, domain);
var list = svc.GetList("Pages");
var listXml = XElement.Parse(list.OuterXml);
var lastModified = listXml.Attribute("Modified").Value;
}

Access SharePoint online using client object model- Forbidden error

I tried to Create a new list item using client object model. I have created an asp.net application to do the task. It works if I pass the URL of SharePoint server which is installed in my machine.
But if I give my SharePoint online URL it is not working as below code shows. I get "The remote server returned an error: (403) Forbidden. " error.
Any idea?
ClientContext context = new ClientContext("https://xxx.sharepoint.com/SitePages/");
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = result.City;
newItem["Body"] = result.State;
newItem.Update();
context.ExecuteQuery();
if you are trying to get a Context object from SharePoint Online you have to put in the right Credentials, as for SharePoint Online you should use the SharePointOnlineCredentials Class
A possible Authentication Method can be look like this:
private void AutheticateO365(string url, string password, string userName)
{
Context = new ClientContext(url);
var passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
Context.Credentials = new SharePointOnlineCredentials(userName, passWord);
var web = Context.Web;
Context.Load(web);
Context.ExecuteQuery();
}
I would imagine you just have to supply your login credentials and it should work:
clientContext.Credentials = new NetworkCredential("Username", "Password", "Domain");
You'll need to including System.Net:
using System.Net;

Apply masterpage to multiple site collections

I need to apply a masterpage to multiple site collections in SharePoint 2010. The masterpage is part of a feature of a VS 2010 solution. The problem is, I can only use site and web scope, not web application. When I use the following code, I get an error that an .aspx website could not be found. Does anybody have a hint why?
SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
string masterUrl = CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + customMasterpage;
string customUrl = CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + customMasterpage;
CurrentWeb.Update();
SPWebApplication webApp = CurrentWeb.Site.WebApplication;
System.Diagnostics.Debugger.Break();
foreach (SPSite site in webApp.Sites)
{
foreach (SPWeb web in site.AllWebs)
{
ChangeMasterPage(web, masterUrl, customUrl);
}
}

Accessing list from Sharepoint Webservice with Sharepoint Online 2013

I am trying to access a list from sharepoint via the web services.
I have tried lots of different web reference URLS for my web service.
The list is found at :
example.com/sites/dms/_layouts/15/start.aspx#/Lists/Documents/AllItems.aspx
the Web service URL I am using now is
https://example.com/sites/dms/_vti_bin/lists.asmx
Obviously example.com is not the real URL.
when I run the code
service.GetList("Documents");
I get the error:
List does not exist.
The page you selected contains a list that does not exist. It may have been deleted by another user.
0x82000006
My full code (many things are just for testing purposes):
public void UpdateList()
{
MKLists.Lists service = GetService();
string targetSite = "https://mywebpage.com/sites/dms";
using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
{
if (ctx != null)
{
ctx.Load(ctx.Web); // Query for Web
ctx.ExecuteQuery(); // Execute
string test = (ctx.Web.Title);
}
}
CookieCollection authCookie = ClaimClientContext.GetAuthenticatedCookies(targetSite, 925, 525);
service.CookieContainer = new CookieContainer();
service.CookieContainer.Add(authCookie);
XmlNode tester = service.GetList("Documents");
}
private MKLists.Lists GetService()
{
MKLists.Lists myService = new MKLists.Lists();
myService.Credentials = System.Net.CredentialCache.DefaultCredentials;
return myService;
}
change this line:
MKLists.Lists service = GetService();
with
MKLists.Lists service = new MKLists.Lists();
i hope this helps.
Edit
according to your comment in the answer here is the update #Michael
try changing your targetsite url to
string targetSite = "https://mywebpage.com/sites/dms/_vti_bin/Lists.asmx";
hope this time it helps
IT turns out it was to do with the subsites.. and this line solved it:
service.Url = "https://mywebpage.com/sites/dms/_vti_bin/lists.asmx";
I've found some users with the same issue.
They said that these links below solved that issue. Could you try it?
http://blogs.msdn.com/b/sharepointdev/archive/2011/05/12/connecting-to-sharepoint-online-web-services.aspx
http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx

Categories