I have used Javascript and sharepoint client object model to add a list item to a list. I am working with a sharepoint 2013 online public site. I have a content editor with the following javascript code.
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Subscriptions');
var email = document.getElementById('email').value;
// Create a new list item
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
listItem.set_item('Title',email );
listItem.update();
This code works fine for the user who has permission. it will add a element to the subscription list. But it fails when we use this for a Anonymous user. After searching I found that that there is a tool : http://anonymous365.codeplex.com/.
But it did not work from the code though i gave anonymous access to the list.
Please suggest me a way to overcome this.
Thanks,
Access to SharePoint Online requires an authenticated session of some variety. The only site that is available for public (anonymous) access is the pre-provisioned "public site" that is available with certain subscriptions which is meant to host a standard corporate website. A sample project that shows how to support claims authentication with SPO can be found here: http://code.msdn.microsoft.com/office/Remote-Authentication-in-b7b6f43c/
Related
Using data connection in SharePoint 2013, or any other method, is it possible to submit form data with each form completion to an external landing place, like a list in SharePoint 365 online? I am new to SharePoint and am needing to learn the ropes pretty quick! Thanks for any help you can provide.
Have a great 2020!
You could develop a custom WebPart or Add-in for SP 2013 and in c# code of this project You could, using SPOnline CSOM, add items to SP Online.
The main problem is the authentication model You are using in SP-OnPrem and SP-online (OF365 tenant). If You have the same kind of authorization provider (for example in both places You are using adfs) then there should not be much of a problem to (as the same user account) submit some data from OnPrem to Online with some Add-in or Webpart as the user will be authenticated in both places. Otherwise You can submit some data from OnPrem to Online with CSOM but You will need to login (in code.. like hardcoded) as some user. This is not the best option from security point of view.
Here is an example how to login to SP Online using CSOM (You could do something very similar in the webpart or Add-in).
... so this are only ideas but generally to do this kind of logic first You need to figure out how You authenticate between OnPrem and Online. If You authenticate the 'same way' You could just create the context of SPOnline and then add/delete or update items with the user permissions. Otherwise You need to authenticate to SPOnline as some user to submit data.
If you use the OOTB list form, we can create an event receivers and use CSOM C# code to add new item to SharePoint Online list. The following code for your reference.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Security;
using Microsoft.SharePoint.Client;
namespace SharePointProjectER.CustomListER
{
/// <summary>
/// List Item Events
/// </summary>
public class CustomListER : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
if (properties.List.Title == "CL0106")
{
//add item to SharePoint Online list
string siteUrl = "https://tenant.sharepoint.com/sites/team";
string userName = "admin#tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
var credential = new SharePointOnlineCredentials(userName, securePassword);
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = credential;
List oList = clientContext.Web.Lists.GetByTitle("CL0106");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = properties.ListItem["Title"];
oListItem.Update();
clientContext.ExecuteQuery();
}
}
}
}
Or we can create a custom web part with custom form or add-in as Adam's reply, and use the CSOM C# code to achieve it.
Reference: Complete basic operations using SharePoint client library code
I am going thru the examples here which shows how to perform basic operations with the SharePoint 2013 .NET Framework client object model (CSOM).
https://msdn.microsoft.com/en-us/library/office/fp179912.aspx
My question is why I don't need to authenticate the user before i can execute query in CSOM
For example
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// The SharePoint web at the URL.
Web web = context.Web;
// We want to retrieve the web's properties.
context.Load(web);
// Execute the query to the server.
context.ExecuteQuery();
// Now, the web's properties are available and we could display
// web properties, such as title.
label1.Text = web.Title;
You dont need to do this cause it will use the current logged in user. Otherwise you have to impersonate the user first.
ClientContext context = new ClientContext("http://server/");
context.Credentials = new NetworkCredential("user", "password", "domain");
// Some Cording
context.ExecuteQuery();
I'd like to be able to programmatically retrieve a user's name and manager using code similar to the following:
using (var clientContext = TokenHelper.GetClientContextWithAccessToken(hostWeb.ToString(), accessToken))
{
var peopleManager = new PeopleManager(clientContext);
var personProperties = peopleManager.GetPropertiesFor(loginName);
clientContext.Load(personProperties);
clientContext.ExecuteQuery();
property = personProperties.UserProfileProperties[requestedProperty].ToString();
return property;
}
On an app without the User Profiles (Social) permission, I get an Access Denied on the ExecuteQuery(). I assume you need the User Profiles (Social) | Read permission on the app to be able to do this. I attempted to side-load an app with this permission on a dev site and was told I needed to be a Tenant Administrator which now has me doubting whether this permission is valid for a store-based app.
Is it possible to access user data such as PreferredName and Manager using the PeopleManager in an app on the SharePoint AppStore? If not, is it possible to access this data in a non-interactive fashion using Microsoft Graph?
You can get this data using the Microsoft Graph, with simple queries like https://graph.microsoft.com/v1.0/me to get the current user profile and https://graph.microsoft.com/v1.0/me/manager to get the manager. You can find a code sample of the Microsoft Graph .Net SDK here: https://github.com/microsoftgraph/aspnet-snippets-sample
Hi do i access a sharepoint list with anonymous authentication? I am trying to read a sharepoint list with javascript and it only works if I have logged in with an admin user but if I have not logged in I get the "Access denied. You do not have permission to perform this action or access this resource." error message.
My code is as below
var siteUrl = "http://site/subsite";
var clientContext = new SP.ClientContext(siteUrl);
var clientContext = new SP.ClientContext.get_current;
var oList = clientContext.get_web().get_lists().getByTitle('Subscriptions');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<view><Query><Where><Contains><FieldRef Name=\'EMail\'/><Value Type=\'Text\'>searchText</Value></Contains></Where></Query></view>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
I believe the site is set to allow anonymous authentication. The subsite also. The list also allows anonymous user to view items. I am using sharepoint 2013
By default the site is not set to allow anonymous authentication. You have to allow anonymous authentication explicitly from IIS.
Please check if you are able to browse that site with the anonymous user.
By default getitems method of list is blocked on SharePoint. You will have to open it using power shell script.
https://sharepointinaction.wordpress.com/tag/the-method-getitems-of-the-type-list-is-blocked-by-the-administrator-on-the-server/
I use the sharepoint lists as a database.
I want to somehow impersonate as different user inside the webpart code
and than as this user I will have both write and edit permission to the list.
My goal is to be able to have full premission only through the webpart code.
I am using MOSS 2007.
SPSecurity.RunWithElevatedPrivilieges() will execute your code as the system account, i.e. the account under which the application pool runs, which might or might not be what you want to do. For example, if you have a workflow attached to the list which is supposed to trigger when new items are added to the list, it will not fire if you insert a new list item under the credentials of the system account (this was a security fix introduced in SharePoint 2007 SP 1). In that case you will have to perform the insert operation under a different account that has the correct permissions on the list.
You can get the UserToken for any user using the following code:
SPUserToken userToken = null;
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
userToken = web.AllUsers["domain\\username"].UserToken;
}
}
});
Replace the "domain\username" with the correct windows account name. Then you can pass this user token to one of the overloads of the SPSite object constructor to execute the code under this user's credentials like so:
using (SPSite site = new SPSite(SPContext.Current.Site.ID, userToken))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
// This code will execute under the credentials of the userToken user
}
}
Hope this helps.
You are looking for SPSecurity.RunWithElevatedPrivileges Method.