How to get userId for custom action in SharePoint - sharepoint

When implementing a List Menu Custom Action in Sharepoint 2013 I have problem getting which user has actually executed the custom action. I would like to do that to check that the user has permission to do this by checking which AD-groups the user is a member of. When I try to resolve the user with the Tokenhelper class it will only return the SharePoint system account sending the query to the Host Web. Is there any way you could attach the userId in the UrlAction parameters like below?
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="77cb35d9-1bf3-4d81-91cf-8a92473a6092.MenuItemCustomAction1"
RegistrationType="List"
RegistrationId="101"
Location="EditControlBlock"
Sequence="10001"
Title="$Resources:TogglePublic"
HostWebDialog="TRUE"
HostWebDialogHeight="250"
HostWebDialogWidth="700">
<UrlAction Url="~remoteAppUrl/CustomActionTarget.aspx?StandardTokens}&SPListItemId={ItemId}&SPListId={ListId}&SPSource={Source}&SPListURLDir={ListUrlDir}&SPItemURL={ItemUrl}&SPUserId={_spPageContextInfo.userId}" />
</CustomAction>
</Elements>
On the Host Web the following code should get the SPUserId:
public partial class CustomActionTarget : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
string userId = this.Request.QueryString["SPUserId"];
}
}
The _spPageContextInfo.userId is a javascript variable that should contain the current user, but I have not found any way to actually resolve and send the userId to the SharePoint App's Host Web using the UrlAction.

Related

Url scheme based link does not seem to work in Blazor MAUI

I am building an app with .NET MAUI and Blazor, that initially targets iOS, but should also support Android, in a next release.
I have, in my Info.plist file added an entry myapp in the CFBundleURLSchemes array. And I use this as a redirect uri from our web portal (open in app, with the href myapp://settings/profile).
What happens, is that iOS comes and asks confirmation if that link can be opened with my app. (see screenshot).
But it just opens the app to the page that was previously open. It does not navigate to the Blazor page that is registered with the #page "/settings/profile" directive.
Is this something that is not supported? Or do I have to add something around the routing, here?
Current logic
With the following code in AppDelegate (for iOS), I can intercept that call and access the requested Url from that scheme-link.
public override bool OpenUrl(UIApplication application, NSUrl url, NSDictionary options)
{
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
if (url?.Scheme?.Equals(myScheme, StringComparison.InvariantCultureIgnoreCase) ?? false)
{
var pageUrl = url.ToString().Replace($"{myScheme}://", "");
PageSettings.RequestedUri = pageUrl; // This is the static class/var I want to leverage in BlazorWebView
return base.OpenUrl(application, new NSUrl( pageUrl), options);
}
return base.OpenUrl(application, url, options);
}
However, I don't seem to find out how I can enforce the BlazorWebView to navigate to the right uri.
As I know, there is no way to do this currently with MAUI Blazor.
Refer to the documentation maui-blazor documentation, the .NET MAUI Blazor hybrid project template isn't a Shell-based app.
If you want to route to #page "/settings/profile", you could describe some information about where to go in AppDelegate, then set some staic and launch a simple page (MAUI PAGE), get the value and show the Blazor page.
public override bool OpenUrl (UIApplication app, NSUrl url, string sourceApp, NSObject annotation){
if (url.BaseUrl.Host.Equals ("app.myapp.io")) {
UIViewController page = new TargetPage().CreateViewController();
}
return true;
}
Set the homepage to your needs
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:BlazorDemo"
x:Class="BlazorDemo.TargetPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/profile.html">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</BlazorWebView>
</ContentPage>

Sharepoint Online Edit Form Toolbar

I'm trying to create a custom action on the edit form on Sharepoint Online with a Sharepoint Add-in..
According to the documentation, I should be able to use EditFormToolbar location as follows:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="965225e0-662b-4089-acdc-78433528f646.TestMenuAction"
RegistrationType="List"
RegistrationId="{$ListId:Lists/Add-in List Test;}"
Location="EditFormToolbar"
Sequence="10"
Title="Test Button">
<!--
Update the Url below to the page you want the custom action to use.
Start the URL with the token ~remoteAppUrl if the page is in the
associated web project, use ~appWebUrl if page is in the app project.
-->
<UrlAction Url="~appWebUrl/Pages/LookupWebPart.aspx?{StandardTokens}&SPListItemId={ItemId}&SPListId={ListId}" />
</CustomAction>
</Elements>
But this fails to deploy with the following error:
#"Error 1
CorrelationId: 817f7325-e9bc-41da-ae9f-400b459ce1cf
ErrorDetail: There were problems with the app web definition in the package.
ErrorType: App
ErrorTypeName: App Related
ExceptionMessage: Deployment failed in host web https://<redacted>.sharepoint.com/sites/dev.addin for app <redacted>/ca27c77f-c56f-409d-a69d-7064091fdda4. Microsoft.SharePoint.SPException: Feature definition with Id ca27c77f-c56f-409d-a69d-7064091fdda5 failed validation, file '/elements33b5f3d1-0607-455b-b292-880b39ec127d.xml', line 6, character 17: The 'Location' attribute is invalid - The value 'NewFormToolbar' is invalid according to its datatype 'http://schemas.microsoft.com/sharepoint/:CustomActionLocations' - The Enumeration constraint failed.
The schema documentation instead suggests that only CommandUI.Ribbon and EditControlBlock are supported locations.
Does anyone know if it's possible to customise the Edit Toolbar in Sharepoint Online?

mvc 5 controlling authorization for entire controller or entire view folder

I have a mvc 5 application with a product controller and a corresponding view folder for all product related views. I'd like to make this entire thing only accessible to user with username "admin" instead of manually assigning a tag [Authorize(Users = "admin")] on top of every controller method. I tried to add a new web.config in Product views folder with the following content, but didnt work.
<configuration>
<system.web>
<authorization>
<allow users="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
Is this possible to achieve at all if yes how?
thanks in advance
The AuthorizeAttribute can be added both on a specific method and on the controller class. The second will affect every action method in that controller.
[Authorize(Users = "admin")]
public class AdminController
{
public ActionResult Index() { ... }
}
If you want to use Web.config you must remember that IIS will load the Web.config file based on the URL without any knowledge of Views. So if the URL is http://server/Products/Something then you must place your configuration file in a folder called Products in the application root. Alternatively you can use <location path="/Products"> in your root Web.config file.

WSS 3.0 ItemAdded

I am working with WSS 3.0 and trying to set some permissions with the ItemAdded-Event.
The problem is, that the event dont fire if I add an item. I've searched the whole web and found nothing. It is only on ItemAdding / ItemAdded, yet I have already implemented a ItemUpdating and ItemDeleting Event which are working perfect! Are there known issues?
This is my Event (It not even get my debugmessages.. :( ):
public override void ItemAdded(SPItemEventProperties properties)
{
Debug.WriteLine("ItemAdded started.");
PMDB_ContentType_Class pmClass = new PMDB_ContentType_Class();
pmClass.SetPermissions(properties);
Debug.WriteLine("ItemAdded ended.");
}
This is the feature.xml
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="34e2c6bd-0e9f-4a65-b280-3cd4c5ff5cb4" Title="PMDB_ContentType"
Scope="Site" Version="1.0.0.0" Hidden="FALSE" DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="PMDB_ContentType\PMDB_ContentType.xml" />
</ElementManifests>
</Feature>
1 >> I hope that you have attached you event with your list properly in FeatureActivated Event receiver of Feature. I cannot check that because you haven't post that code. please check if there are any errors in that code.
2 >> Use Sharepoint Manager (http://spm.codeplex.com/releases/view/51438) to see if your event is properly attached to your list or not. if not then try to deactivate feature from web UI and reactivate it again.

I've got a sharepoint solution, what next

I'm new to sharepoint. I have a C# solution, that has masterpages and user-controls to be used in a sharepoint site. I have setup my sharepoint dev VM and i can browse the default sharepoint stuff.
How do I add the master pages to Sharepoint? Where do I go from here?
My suggestion would be to deploy the master pages as a feature rather than a manual process. Solutions (WSPs) and Features are the supported way to deploy content/features into sharepoint. A really great tool for sharepoint development is called WSPBuilder
A master page is deployed into sharepoint as a "module" that you will place into your elements.xml file in the feature.
Think of a solution as a .cab file with a different extension. Within that is a file called feature.xml which defines the title of your package when its deployed. Features can be activated and deactivated to deploy and undeploy your content into parts of your farm.
Here is an example of a css file deployed as a Module... Master pages would be similar however, they would deploy into the master page gallery rather than the style library. This module deploys a custom css file into the "Style Library" of a site collection. After this is deployed I used a "Feature Receiver" (event handler) to grab a reference to the SPSite object and modify its alternate stylesheet so that my override took place.
Feature.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="63BB13A0-1F9C-4c3b-BE60-10E59CEE0113"
Title="Custom CSS Feature"
Description="Deploying a custom CSS using a feature"
Version="1.0.0.0"
Hidden="FALSE"
Scope="Site"
ReceiverAssembly="CustomCSSFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24f1377a8414d2ed"
ReceiverClass="CustomCSSFeature.FeatureReceivers.CustomCSSFeatureReceiver"
>
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>
elements.xml - you'd modify this to reflect where master pages are supposed to be deployed I would think that this is the Url property. The Path="Styles" refers to the relative path within the feature itself where the style sheet resides (e.g. in your visual studio i have a sub folder called styles beneath the folder called CustomCSSFeature and that is where the style sheet exists)
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="OSGStyles" Url="Style Library" Path="Styles" RootWebOnly="TRUE">
<File Url="custom-css.css" Type="GhostableInLibrary" />
</Module>
</Elements>
Then, in my feature receiver class I have activated/deactivated handlers which "apply" the stylesheet to the publishing web. In your case you can likely change the default master page for the website in a feature receiver as well.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.OpenWeb())
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
publishingWeb.AlternateCssUrl.SetValue(web.ServerRelativeUrl +
"/Style Library/custom-css.css", true);
publishingWeb.Update();
web.Update();
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.OpenWeb())
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
publishingWeb.AlternateCssUrl.SetValue("", true);
publishingWeb.Update();
web.Update();
}
}
Copy them to SharePoint root (For SP 2007 default location is C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\, for SP 2010 instead of "12" you have "SharePointRoot")
From there, copy your files to \TEMPLATE\LAYOUTS folder and then you can reference masterpage from your aspx pages like "/_layouts/mymasterpage.master".
UserControls goes into \TEMPLATE\CONTROLTEMPLATES
Get to know the directory structure in the 12 Hive
Exploring the 12 Hive : TEMPLATE Directory
Another way is to put your masterpage in master pages list. Use this link to access master page list and upload your masterpage: http:///_catalogs/masterpage
You can add master pages using the SharePoint Designer 2007.
Generally i recommend you to take a look at the answers of this question: Learning Sharepoint

Resources