Saturday, 1 August 2020

Implement Custom SuiteBarBranding Delegate Control In SharePoint 2013

Introduction

In this article we will see how to implement SuiteBarBranding Delegate Control.

SuiteBarBrandingDelegate: This delegate care about top left site logo and text. Facilitates us to override the left-top corner text for the site. This text can only be replaced by overriding SuiteBarBrandingDelegate Delegate Control with custom control created using Visual Studio. Normally, there’s a text reading "SharePoint", so we can replace it with the title of our own site:

SuiteBarBrandingDelegate Control will look as follows in SharePoint Page.

Header before

Header

Step 1: Create one empty SharePoint Project and provide the Solution Name and choose the Solution Path. Then click OK.

Step 2:
 Deploy the Solution as Farm Solution. Provide the Url in next screen and validate the connection.

Step 3: Add New UserControl to the Project from the Templates and provide a Name to it. In our case it is “MyCustomSuiteBarBrandingDelegate

UserControl

Now our Solution Explorer looks as follows,

Solution Explorer

Step 4: Now open the “.ascx” of the User Control and paste the following code.

User Control

Step 5: Code snippet for the ascx.cs file.

Code snippet

Step 6: Add Elements.xml file to the Solution, provide the name and click  Add.

Empty element

Step 7: Click on elements.xml file and paste the following code snippet inside the elements tag.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.   <!-- Adding DelegateControl reference to our custom SuiteBarBrandingDelegate Control -->  
  4.   <Control ControlSrc="/_controltemplates/15/SuiteBarBrandingDelegate_Example\MyCustomSuiteBarBrandingDelegate.ascx"  
  5.            Id="SuiteBarBrandingDelegate"  
  6.            Sequence="1" />  
  7. </Elements>  
Step 8: Final step is build and deploy then the SharePoint text will be overridden with the text of the portal.

The final outcome will be as in the following screenshot,

final Outcome

Hide Link Left Navigation In SharePoint 2013 On Permissions Basis Using jQuery

Introduction

In this article we will explore how to hide link left navigation in SharePoint 2013 on permissions basis using jQuery. There are two ways to remove link from Quick Launch in SharePoint 2013.

  1. To customize the navigation, just click EDIT LINKS.

    edit links
    Click EDIT LINKS to customize the top navigation.

    Click EDIT LINKS to customize navigation in the Quick Launch.

    Note: If you don’t see the option to edit links, you probably don’t have permission to customize the navigation. To customize the navigation, you need to be a site owner or have permission to customize the site.

  2. Click Settings > Site settings. On the Site Settings page, click Navigation and to delete a heading, select the heading you want, and then click Delete.

    settings

    Note: These additional navigation settings are available only if the publishing features are enabled for your site collection.

Cause

To get content of a site to be published on the basis of permissions granted, need to make the changes through CSS or JS added to the master page. But in this case we need to change the master page. The other way is, do through console application include Custom Actions, global JS files "Script Link" using CSOM code without even touching the master page and SharePoint designer.

Example

In a company some of the employees work on contract basis, they don’t have any employee ID assigned to them and not able to access the whole information available on the portal.

So here, we are going to show the information (e.g. navigation links) on the basis of permission granted to the user.

test2013

The following is the implemented solution:

Cs.code

cscode

Uploaded style Library script

Uploaded style Library script

Final Output

Final Output

SharePoint 2013 And "In-List" Find/Searches

Introduction

In this article, we will explore how SharePoint 2013 natively provide the ability to find content within the current list.

Scenario

Created a custom list called “Divisions” and you can see the search box here that allows me to find or search for a specific Division within the list. This prevents me from doing an actual “search” using the site search box and receiving all results related to one division from the entire search index.

Find item

Division

However, this functionality is not available in every list type. In fact, there are four list types that do not allow this natively. For your reference, here are all of the list types broken down by those that do and do not allow in-list finding /searching.

Allow in-list finding /searching

  1. Announcements List
  2. Calendar List (“All Events” view)
  3. Contacts List
  4. Content and Structure Reports List
  5. Custom List
  6. Document Library
  7. Forms Library
  8. Issue Tracking List
  9. Links List
  10. Picture Library
  11. Promoted Links List (“All Promoted Links” view)
  12. Reusable Content List
  13. Site Assets Library
  14. Site Collection Documents Library
  15. Site Collection Images Library
  16. Site Pages Library
  17. Style Library
  18. Tasks List
  19. Workflow Tasks List

Do not allow in-list finding /searching

  1. Discussion Board List
  2. MicroFeed List
  3. Survey List
  4. Wiki Pages Library

SharePoint 2013 - Implemented RunWithElevatedPrivileges With Apps (S2S)

Elevate User Access with App Only Policy

Scenario

The reason we use RunWithElevatedPrivilages is to execute our code with elevated permission regardless of current login user permission.

By default, SharePoint Apps run in context of user + app which means current user and the app both should have sufficient rights to access SharePoint resources. But in some cases we need our app to access SharePoint resources regardless of current user permission, this is where AppOnlyPermssion comes into picture. In this article I will let you know how to use it.

Solution

To make an App performing work that the user does not have permission to means we will use the app only policy. To enable the App Only policy in your app, you have to add the "AllowAppOnlyPolicy" attribute to your "AppPermissions" element in the App Manifest:
  1. <AppPermissionRequests AllowAppOnlyPolicy="true">   
  2. </AppPermissionRequests>  
This capability is only available to provider-hosted apps. It is not available to SharePoint-hosted apps. In a SharePoint-hosted app, there is Full trust code and is not limited by permissions – it can do anything it wants.

S2S (High Trust) - App Only Context

An S2S access token by calling the GetS2SAccessTokenWithWindowsIdentity method of the TokenHelper class. Use the TokenHelper::GetS2SAccessTokenWithWindowsIdentity method, passing a null for the WindowsIdentity parameter.

On-premises farm

Let’s see how we can actually utilize the App Only policy to elevate user permissions. I have written the following code in the code behind of the TestPage.aspx of S2S Provider Hosted App.

Code:
  1. Uri _hostWeb = new Uri(Request.QueryString["SPHostUrl"]);  
  2. string appOnlyAccessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(_hostWeb, null);  
  3. using(ClientContext clientContext = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(_hostWeb.ToString(), appOnlyAccessToken))  
  4. {  
  5.     List Testlist = clientContext.Web.Lists.GetByTitle("TestList");  
  6.     ListItemCreationInformation info = new ListItemCreationInformation();  
  7.     Microsoft.SharePoint.Client.ListItem item = Testlist.AddItem(info);  
  8.     item["Title"] = "Created S2SApp";  
  9.     item["Body"] = "Created from S2S” + DateTime.Now.ToLongTimeString();  
  10.     item.Update();  
  11.     clientContext.Load(item);  
  12.     clientContext.ExecuteQuery();  
  13. }  
Deploy the app and then log on as a user that only has read permission to the list. Execute the code, and a new item is created even though the user does not have permission to create items in the list. The Created By and Modified By fields in the list will reflect that it was the SHAREPOINT\App account that were used to create the item.

Note:

As you can see, the ClientContext that is opened with the appOnlyAccessToken will run with the identity of the SHAREPOINT\App account. This is a very good practice to remember even when using RunWithElevatedPreviledges in the Server Object Model.

References:

"SharePoint 2013 App Only Policy Made Easy"

New Delegate Control Additions To SharePoint 2013

Introduction

In SharePoint 2013, three new delegate controls have been introduced for the purpose of displaying the new top Suite bar (with links SkyDrive, NewsFeed, Sync, follow, etc).

Here are the controls,

  • PromotedActions Delegate Control
  • SuiteBarBrandingDelegate delegate Control
  • SuiteLinksDelegate delegate Control

So let’s take a look at where these controls are placed on the Master page and how we can replace them.

PromotedActions

Delegate Control is responsible for displaying Links “Share, Follow, SYNC, EDIT” in top right below SuiteLinksDelegate Control. These links can be replaced by Overriding PromotedActions Delegate Control using a Custom Control created using Visual Studio to the following area on a SharePoint site in the top-right section of the page.

PromotedActions

An example of adding an additional link may look like the following:

output

SuiteBarBrandingDelegate

Delegate Control is responsible for displaying ‘SharePoint’ or ‘Office 365′ text on top left of the new SharePoint 2013 site (in the blue bar). This text can only be replaced by overriding SuiteBarBrandingDelegate Delegate Control with a Custom Control created using Visual Studio. Normally, there’s a text reading "SharePoint" like the following:

Header before

Header

After apply delegate control


delegate control

SuiteLinksDelegate

Delegate Control is responsible for displaying Links “NewsFeed, SkyDrive and Sites” in top right of the new SharePoint 2013 site (in the blue bar). These links can be replaced by overriding SuiteLinksDelegate delegate  ontrol using a Custom Control created using Visual Studio. The SuiteLinksDelegate control will allow us to modify the default links, and to add our own links, in the "suit links" section.

Before

SuiteLinksDelegate

Replaced with

Replace with

Applying REST Query Filter To Field With The SharePoint 2013 REST API

Introduction

SharePoint development using JavaScript for any time I require unique customization not possible using out of the box. One of the technical challenges you may come across in your travels is trying to perform a GET of list items and filtering where a column value. Seems pretty straightforward right?

Scenario

In old days working with SharePoint lists or library, many times we came across querying lists and applying filter on list items using CAML Query. We all know constructing long CAML query gets confusing and many lines of chunks as well.

But no worries REST is the life saver; with REST it’s very easy to filter lists and gives to flexibility using few lines or sometimes just on line of code.

In this post, I will be showing how you can use REST to filter or query on SharePoint lists and returns data as you want.

You’ll first start to craft your URI like the following:

https://<siteUrl>/_api/Web/Lists/GetByTitle(‘listname’)/items?$select*&$filter= Title eq ‘test’

  • $Select: Lets you select list columns you want to return
  • $filter: Condition based results.

Refer the following images and code for clear understanding.

divisions
Solution

1st Approach

Open the browser and add the following URL in the address bar:

https://hostssite/apps/TestAppsite/_api/Web/Lists/GetByTitle('Divisions')/items?$select=*&$filter=Title eq 'test1'

code

2nd Approach: Using JQuey

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts “dialogue, go to the "Media and Content" category, select the "Script Editor" Web Part and click the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following code snippet:

  1. function Divsion ()  
  2. {  
  3.   
  4.     var listName = "Divisions";  
  5.     var itemId="";  
  6.   
  7.     var Ownurl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle(' Divisions’)/items?$select=OwnerEmailID&$filter=Title eq 'test1'";  
  8.     $.ajax({  
  9.   
  10.         url: Ownurl,  
  11.         headers: { Accept: "application/json;odata=verbose" },  
  12.         async:false,  
  13.         success: function (data) {  
  14.             var items = data.d.results;  
  15.             if (items[0].OwnerEmailID != "") {  
  16.                 itemId = items[0].OwnerEmailID;  
  17.             }  
  18.   
  19.   
  20.         },eror: function (data) {  
  21.         alert("An error occurred. Please try again.");  
  22. }  
  23.     });  
  24. }  

 

Sharepoint 2013: Display User Login Name As Notification Dialog Box

Scenarios

When we upload the existing document in the Document Library by unchecking the override existing file,we get a SharePoint OOTB exception saying "A file with the same name already exists and it was last modified by i:o#.w|domain\username."

In this case we have to trim the i:o#.w| which is SID or represents type of authentication used.

Refer to the below screenshot for a clear understanding of the requirement.



Figure 1: Requirement



Figure 2: After

Solution

Step 1: Open the site collection in browser. Go to SiteSettings, MasterPage and then check the selected Site Master Page and System Master Page.


Figure 3: Site collection

Step 2: Open the site in SharePoint Designer. Go to All Files, _catalog, then masterpage.



Figure 4: Designer

Step 3: Open any one of the selected master page in step 1 in advance mode.

Step 4: Add the following script before </head> tag. Check screenshot for reference.

  1. <SharePoint:ScriptLink ID="ScriptLink_CustomUploadpage" name="~sitecollection/Style Library/Scripts/CustomUploadScript.js" runat="server" LoadAfterUI="true" Localizable="false"/>


Figure 5: Screenshot for reference

Step 5: Repeat step 4 for the other master page selected in step 1.

Step 6: Come to browser and open style library by going to site content, then style Library.

Step 7: Upload the CustomUploadScript.js file to script folder in style library.



Figure 6: Script

Step 8: Test and Verify.

SharePoint 2013 - Uploading Multiple Attachments To The New Item On List Using JSOM And REST API

  Introduction In this article, we will explore how we can attach multiple attachments to the new item on list using JSOM and REST API. Ther...