Aug 19, 2016

Simple Workaround for Content Editor Webpart in Another Site Collection

Guess what? You have created a HTML script stored somewhere in SharePoint, and you want to use it somewhere in another Site Collection. But you are stuck with this error, “Cannot retrieve the URL specified in the Content Link property. For more assistance, contact your site administrator”.

image

There are a few workarounds you can try.

Using Page Viewer or IFRAME

To use the page viewer, the solution is quite straight-forward and can be described in a few sentences.

Firstly, you know that you have created the HTML script, now create a page in the same Site Collection, then add Content Editor Webpart that links to the HTML script.

Then you can go to the other Site Collection, create a page with Page Viewer, or direct HTML with IFRAME which links to the page created in previous step. Remember, the SharePoint top links bar and left navigation will still be displayed. To overcome that, you can add query string “IsDlg=1” at the end of the URL.

If your URL is like: http://contoso.com/Pages/cewp.aspx, then it should be like http://contoso.com/Pages/cewp.aspx?IsDlg=1.

Using jQuery

If your masterpage is attached with jQuery, you can re-use the following code.

Where "/SiteAssets/common/thescripts.txt" is the URL of your HTML script.

Aug 8, 2016

Tips: Open Office Document Online (Web Version) in SharePoint

In SharePoint, there are 2 ways of setting a link to an Office Document. The most common way we’ve always seen is by direct link, which will download the Office document to your local storage.

Now, there’s another way to link to an Office document and force it to open in Online viewer such as Excel Online, PowerPoint Online, or Word Online. The most possible way is to copy the URL by clicking on the ellipsis next to the filename.

image

Now, you’ll see something similar to this, which will force-open the file in Excel Online:
https://www.contoso.com/Shared%20Documents/Essential%20Regulatory%20Documents%20Inventory%20List%20Template.xlsx?d=we035ad79215c4e82ba32d989b66bacfd

Or at any point of time if you know the direct URL to the file but unsure what is the number or code next to d=, you can just put the link as below.

https://www.contoso.com/Shared%20Documents/Essential%20Regulatory%20Documents%20Inventory%20List%20Template.xlsx?web=1

This way, it will forcefully open the excel (or essentially any Office document) in Online Viewer.

 

WAIT, there’s more! I know that you’re going to complain!

Jul 27, 2016

Using jQuery to Send Email from SharePoint

I’ve been outside of this blog so far and never post anything in this blog. But let me share a small code snippet that can be used. Below is the code to send email from SharePoint.

var EmailInfo = { To : { Address : 'radityo.ardi@gmx.com' },
	Subject : 'This is the subject'
	Body : 'This is the Body'
};

$.ajax({
	url: _spPageContextInfo.webServerRelativeUrl + '/_api/SP.Utilities.Utility.SendEmail',
	contentType: 'application/json',
	type: "POST",
	headers: {
		"Accept": "application/json;odata=verbose",
		"content-type": "application/json;odata=verbose",
		"X-RequestDigest": $("#__REQUESTDIGEST").val()
	},
	data: JSON.stringify({
		'properties': {
			'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
			'From': 'Radityo Ardi',
			'To': { 'results': [EmailInfo.To.Address] },
			'CC': [],
			'Subject': EmailInfo.Subject,
			'Body': EmailInfo.Body
		}
	}),
	success: function(dataSendEmail) {
		alert('Email successfully sent.');
	},
	error: function(err) {
		console.log(err);
	}
});

Oct 9, 2015

Snippet: Strip HTML tags using jQuery

After quite a long time I never posted anything on my blog, now I’m ready to back. To begin the adventure again, I’ll post 1 interesting code snippet in jQuery.

Basically it is very hard for us to strip HTML tag in JavaScript, but we also know that jQuery is a powerful framework itself. I didn’t recognize this functionality after today I came into one thing in mind, how to strip HTML tag in JavaScript?

DSC_0017[1]

Now, let us see on the snippet below and enjoy…!

  1. function stripHtml(htmlContent) {
  2.     return $("<p>").html(htmlContent).text();
  3. }

Oct 25, 2013

DateDiff and DateAdd Excluding Weekends in SQL Script

Have you ever wonder how to count date differences and add date, but excluding weekends (Saturday and Sunday)? I stumbled on these 2 functions, which I prefer best to do the operation.

Date Add only Business Day
  1.  
  2. CREATE FUNCTION [VARS].[FN_DateAddBusinessDay]
  3. (
  4.     @InputDate DATETIME = NULL,
  5.     @InputDays INT
  6. )
  7. RETURNS DATETIME
  8. AS
  9. BEGIN
  10.     -- Declare the return variable here
  11.     DECLARE @OutputDateTime DATETIME
  12.     DECLARE @date DATETIME
  13.     DECLARE @days INT
  14.     DECLARE @weeks INT
  15.     DECLARE @i INT = 0
  16.     -- Add the T-SQL statements to compute the return value here
  17.     SET @date = @InputDate
  18.     SET @days = @InputDays
  19.     IF @days >= 1
  20.     BEGIN
  21.         SET @days = @days - 1
  22.     END
  23.     
  24.     IF DATENAME(dw, @date) = 'Saturday'
  25.     BEGIN SET @i = 2 END
  26.     ELSE IF DATENAME(dw, @date) = 'Sunday'
  27.     BEGIN SET @i = 1 END
  28.     SET @date = DATEADD(d, @i, @date)
  29.  
  30.     IF (@days < 0)
  31.     BEGIN
  32.         IF DATEPART(dw, @date) = 1
  33.             SET @date = DATEADD(d, -1, @date)
  34.         SET @weeks = (datepart (dw, @date) + @days - 6)/5
  35.     END
  36.     ELSE
  37.     BEGIN
  38.         IF DATEPART(dw, @date) = 7
  39.             SET @date = DATEADD(d, 1, @date)
  40.         SET @weeks = (DATEPART(dw, @date) + @days - 2)/5
  41.     END    -- Return the result of the function
  42.     SET @OutputDateTime = DATEADD(d, @days + (@weeks * 2), @date);
  43.     RETURN @OutputDateTime;
  44.  
  45. END
  46.  
  47. GO

DateDiff only Business Day
  1. CREATE FUNCTION [VARS].[FN_DateDiffBusinessDay]
  2. (
  3.     @InputStartDate DATETIME = NULL,
  4.     @InputEndDate DATETIME = NULL
  5. )
  6. RETURNS INT
  7. AS
  8. BEGIN
  9.     -- Declare the return variable here
  10.     DECLARE @OutputDays INT
  11.     DECLARE @date DATETIME
  12.     DECLARE @days INT
  13.     DECLARE @weeks INT
  14.     -- Add the T-SQL statements to compute the return value here
  15.     IF DATEDIFF(DD, @InputStartDate, @InputEndDate) >= 0
  16.     BEGIN
  17.         SET @OutputDays = (SELECT
  18.            (DATEDIFF(dd, @InputStartDate, @InputEndDate) + 1)
  19.           -(DATEDIFF(wk, @InputStartDate, @InputEndDate) * 2)
  20.           -(CASE WHEN DATENAME(dw, @InputStartDate) = 'Sunday' THEN 1 ELSE 0 END)
  21.           -(CASE WHEN DATENAME(dw, @InputEndDate) = 'Saturday' THEN 1 ELSE 0 END))
  22.     END
  23.     ELSE
  24.     BEGIN
  25.         SET @OutputDays = 0
  26.     END
  27.     RETURN @OutputDays;
  28.  
  29. END

Enjoy, and hope it helps! Winking smile

Apr 24, 2013

Named String Format in C#

It is nice on how NET Framework gave a bunch of “shortcut” code, including String.Format. But we have to keep track of the indices in the object array which is not really nice.

82653205

It would be good if we just write code, declare a named string collection and format it.

  1. string format = "my name is [[name]] for it called [[name]] to be noted. and address is [[address]].";
  2. Hashtable ht = new Hashtable() { { "name", "Minion No. 10594" }, { "address", "gru's place" } };
  3. txtResult.Text = format.Format(ht);

Now we can use my commonly-used library in rdz.codeplex.com, and use the Format method in StringUtility class. You can download it and use it.

Don’t forget to add reference to Rdz.dll and put using directive as below. You are good to go and the Format function will be available on every string object, like in the example above.

  1. using Rdz.Utility;

In case you didn’t notice, the hashtable is case-sensitive. So to make it case-insensitive, declare Hashtable as below.

  1. Hashtable ht = new Hashtable(StringComparer.CurrentCultureIgnoreCase) { { "name", "Minion No. 10594" }, { "address", "gru's place" } };

Cool! See you! Winking smile

Important Command-Line for Developers

There some bunch of tools which we never know that will help us in our daily operations as developer. But I’ll give you some of that.

Generate Web-Service Proxy Class from WSDL

Generating proxy class from WSDL is important if you want to get data from multiple web services containing exactly the same method. So, in the end you can use the class, set the URL and call the method simultaneously with other URLs.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\WSDL.exe" /out:"<path to cs file>\<cs filename>.cs" /n:<namespace> http://<complete URL to WSDL>/<WebServiceName>.asmx?wsdl

Generate XSD from XML

Generating XSD can be done by using XSD.exe file located in Microsoft SDKs.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\XSD.exe" "<path to xml file>\<xml filename>.xml"

Generate Class from XSD

Generating Class from XSD also useful in many ways. There are 2 type of class, the one which we can use as dataset, and the one just plain class to use to read Xml. So if you want to deserialize an XML and put it on your code, definitely you will need to convert XML to XSD, and XSD to Class.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\XSD.exe" /language:CS /classes /n:<namespace> "<path to XSD file>.xsd"

Apr 6, 2013

Attach SQL Database from Network Drive

One crazy question came up in mind, can I attach a database from network drive? There’s a lot of recommendation, that we can’t attach SQL Database. Why would we want it? Anyway no one would do that, unless they want a little bit hiccup in their system and mess up the whole thing? That’s true, but I want a solid answer. Can it be done?

The answer is it can be done. But again, on really strict rules, this is not recommended. Why would I want it? Because I have development VM (VirtualBox) and I need those big databases (say about ~20GB) to be exist on my system without copying it inside my Virtual Hard Disk which will take some time to copy. Again, it’s a development VM, so I could manage it and no one would care if my system crashed or database not exist.

So, here it is.

Attach Database from Network
  1. -- this command to turn on network share attach DB
  2. DBCC TraceOn(1807)
  3. -- attach it
  4. EXEC sp_attach_db @dbname = N'WSS_Content_85',
  5.    @filename1 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85.mdf',
  6.    @filename2 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85_log.ldf'
  7. -- this command to turn off network share attach DB
  8. DBCC TraceOff(1807)

Please do note that this command would likely not recommended, especially on production server. Thanks to Sumardi Soemartopo for blogging about this COOL tips, and this is the link to that page http://guozspace.wordpress.com/2009/02/26/microsoft-sql-server-how-to-attach-database-from-network-drive/.

Mar 13, 2013

Assign a User to Manage SharePoint User Profile

Maybe in some of particular scenario, we found that the user wanted to manage user profile alone without using Farm Administrator Account and to keep the user with a minimal security access.

That way, we can configure User Profile Service Application to have particular user granted as User Profile Manager. So they can go to Central Administration website without touching other configuration.

How we can do this?

Mar 8, 2013

Xml: Another Way to Use Configuration Options

Many developers believe, a good way to save configuration, can save another line of code. So do I, do not like complicated code to just saving a configuration options for an application that I’ve made. Can’t say how many methods out there to save configuration options, as this is not limited to creativity of each person.

E014584

I do have one way, which is simple enough for me to save any type of configuration with almost any kind of data type. Xml is one of the many options and to do that we just need 2 methods, serialize and deserialize.