Sometimes it’s necessary to know the status of all the pages before a site is made live. It becomes especially difficult when a team works together on multiple pages and there are number of pages which might have not been published and site owner has to make sure all the pages are published.
We will create a view which can help a site owner to view unpublished pages or draft pages or published pages in SharePoint.
The technique is using the “Version” column to determine the decimal part of the “Version” by subtracting the integer part from the “Version”. So if there is any decimal value in the “Version” then it’s in unpublished or draft state.
Do note that this technique depends upon the “Document Version History” settings being “Create major and minor (draft) versions” for that library.
This involves two steps
Add a calculated column which helps to determine the page state using version value.
Create a new view or update an existing view to display the created calculated column.
Create column to know the page status
Create a new column and name it “PageStatus” (We will later rename it to “Page Status”).
Set the type to “Calculated”.
Add the below formula and save the column settings. (Note: The “Version” column will not be available in “Insert Column:” pane so just copy paste the formula.)
In the library settings, click on the column to edit.
Change the column name to “Page Status” and click “OK” button.
Library view to show the page status
We can either create a new view or modify an existing view to show the “Page Status”. For this all that needs to be done is add the “Page Status” column to the view.
Sometimes in SharePoint there will be a scenario where users shouldn’t download documents, but yet should be able to view the documents. In many of the sites it’s mentioned that this is not possible. But in reality as of March 2020, this is possible.
Generally the permission level “View Only: Can view pages, list items, and documents. Document types with server-side file handlers can be viewed in the browser but not downloaded.” is not available by default. But the site collection feature “SharePoint Server Enterprise Site Collection features” when activated will enable this permissions level.
Note that this will work only for Microsoft Office files like Word, Excel, PowerPoint etc. Still users will be able to download other file type. The reason is SharePoint uses handler for viewing and editing Microsoft Office files which can prevent download.
Perform the following steps to enable the permission level
Launch “Site collection features” under “Site Settings”.
Activate “SharePoint Server Enterprise Site Collection features”.
Go to the library’s settings and launch “Permissions for this document library”.
Enable unique permissions.
Then select the specific “SharePoint group” and click “Edit User Permissions”.
Now you should be able to see the permission level ” View Only: Can view pages, list items, and documents. Document types with server-side file handlers can be viewed in the browser but not downloaded.”
Check that permission and uncheck all other permissions.
Now all the users within that group will only be able to view the document in web-viewer and will not be able to download.
Following is the code which quickly prints them out in a table. Just add this code to a script editor web-part.
Please make sure the URL is updated based on your site URL.
For pages with multiple document libraries when you want to target a specific library prepend the id of the web part div and an underscore e.g. WebPartWPQ4_
Sometimes we need Microsoft Office documents to be opened in dialogs instead of SharePoint’s default behavior which is opening the document in the same window or tab.
The following code helps in implementing this.
Note: This code only works for office documents. For pdf documents look at the previous article.
<!-- Put this code below the list view web part -->
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js">
</script>
<script type="text/javascript">
$hrefVal = $("a[onclick*='return DispEx'][target!='_blank']").attr("href");
$siteUrl = _spPageContextInfo.webServerRelativeUrl;
$("a[onclick*='return DispEx'][target!='_blank']")
.removeAttr("onclick")
.attr("href", "javascript:OpenDocumentInDialogue(\"" + $siteUrl + "/_layouts/WopiFrame.aspx?action=default&sourcedoc=" + $hrefVal + "\")");
function OpenDocumentInDialogue(href) {
var o;
$("body").append("<div id='docTemp' style='min-height:600px;min-width:800px;height:100%;overflow:hidden'><object data='" + href + "' width='100%' height='100%'></object></div>");
o = {};
o.html = $("#docTemp")[0];
o.showMaximized = true;
o.title = href.substring(href.lastIndexOf("/") + 1);
o.dialogReturnValueCallback = OpenDocumentinDlgCallback;
SP.UI.ModalDialog.showModalDialog(o);
}
function OpenDocumentinDlgCallback() {
// do something here when the dialog closes
}
</script>
Sometimes we need PDF documents to be opened in dialogs instead of SharePoint’s default behavior which is opening the document in the same window or tab.
The following code helps in implementing this.
Note: This code only works for PDF documents. For office documents look at the next article.
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$("a[href$='.pdf']").each(function() {
$(this).removeAttr("onclick").attr("href", "javascript:OpenDocumentInDialogue(\"" + this.href + "\")");
});
function OpenDocumentInDialogue(href) {
var o;
$("body").append("<div id='docTemp' style='min-height:600px;min-width:800px;height:100%;overflow:hidden'><object data='" + href + "' width='100%' height='100%' type='application/pdf' ></object></div>");
o = {};
o.html = $("#docTemp")[0];
o.showMaximized = true;
o.title = href.substring(href.lastIndexOf("/") + 1);
o.dialogReturnValueCallback = OpenDocumentInDialogueCallback;
SP.UI.ModalDialog.showModalDialog(o);
}
function OpenDocumentInDialogueCallback() {
// Do something here when the dialog closes
}
</script>
The following code helps in displaying the “Working on it…” dialog box available in SharePoint 2013 or SharePoint Online.
Before you being the background operation, call the function “RequestStarted“. Once the background operation has been completed, call the function “RequestEnded“.
function RequestEnded(sender, args) {
try {
waitDialog.close();
waitDialog = null;
} catch (ex) {}
};
function RequestStarted(sender, args) {
var waitDialog;
ExecuteOrDelayUntilScriptLoaded(ShowWaitDialog, "sp.js");
};
function ShowWaitDialog() {
try {
if (waitDialog == null) {
waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('Processing...', 'Please wait while request is in progress...', 120, 440);
}
} catch (ex) {}
};
Sometimes we need to hide specific SharePoint columns in a list view webpart. The following code will help with that.
Make sure this code is placed in a JS file and referenced using JSLink webpart property.
Just replace the HideColumn function’s array parameters [“Title”, “Created By”] with necessary column’s display names.
(function () {
var overrideCurrentContext = {};
overrideCurrentContext.Templates = {};
overrideCurrentContext.OnPostRender = PostRenderer;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
})();
function PostRenderer(ctx) {
HideColumn(["Title", "Created By"],true);
}
function HideColumn(columns, hideOnlyHeader) {
columns.forEach(function(columnName) {
headerParentFirstNode = document.querySelectorAll("[displayname='" + columnName + "']")[0];
if (headerParentFirstNode != undefined) {
var header = headerParentFirstNode.parentNode;
header.style.display = "none";
if (!hideOnlyHeader) {
var index = [].slice.call(header.parentNode.children).indexOf(header) + 1;
for (var i = 0, cells = document.querySelectorAll("td:nth-child(" + index + ")"); i < cells.length; i++) {
cells[i].style.display = "none";
}
}
}
});
}