Get Comment Count in SharePoint


Hi Guys,

In this blog I will explain how we can get the number of comments on any page/article. Prerequisites for this blog are: a list with rating enabled, an article/page/splistItem already have got comments from users and a tool like VS to code 😉

According to requirement it may be the case that one need to get item comment count using java script or c#. I will explain both way to get the comment count:

Using Javascript: This may be the case when one want to use the content search webpart or CQWP to display comment count. Followng code can be use to get the comment Count:

var linkURL = $getItemValue(ctx, “Link URL”);
linkURL.overrideValueRenderer($urlHtmlEncode);

// Get the Comments count
var soapEnv =
“<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance&#8217; xmlns:xsd=’http://www.w3.org/2001/XMLSchema&#8217; xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’&gt; \
<soap:Body>    \
<CountCommentsOnUrl xmlns=’http://microsoft.com/webservices/SharePointPortalServer/SocialDataService’&gt; \
<url>” + linkURL + “</url> \
</CountCommentsOnUrl> \
</soap:Body> \
</soap:Envelope>”;

$.ajax({
pageurl: linkURL,
url: “/_vti_bin/SocialDataService.asmx?op=CountCommentsOnUrl”,
type: “POST”,
dataType: “xml”,
data: soapEnv,
contentType: “text/xml; charset=\”utf-8\””,
success: function(data, status, xhr){
if(data !== undefined) {
var commentCount = $(‘CountCommentsOnUrlResponse’, data).find(‘CountCommentsOnUrlResult’).text();
if(commentCount>0){
$(‘#’+ commentId +  ‘ .comments’).html(commentCount);
$(‘#’+ commentId).show();
}
else{
$(‘#’+ commentId).hide();
}
}
}
});

Using server code: You must have to know the full path of the page. That is again a challenge. In most of the cases you must be having SPListItem object. Following code can be used to get the path:

var pageUrl = item[“EncodedAbsUrl”] + item[“FileRef”].ToString().Substring(item[“FileRef”].ToString().IndexOf(“#”) + 1);

var context =SPServiceContext.Current;
var scManager =newSocialCommentManager(context);
var pageUri =newUri(pageUrl);
int count = scManager.GetCount(pageUri).ToString(CultureInfo.InvariantCulture);

Hope above code helped you. Kindly comment and share your views.

Happy SharePointing 🙂

 

Schedule daily SQL Database Backup


Hi guys,

In practical cases we require backup and restore facility to our database which provides an essential safeguard for protecting critical data stored in SQL Server databases. To minimize the risk of catastrophic data loss, you need to back up your databases to preserve modifications to your data on a regular basis. Backup and restore strategy always protects databases against data loss caused by a variety of failures.

Purpose and Scope:

In this blog, I am going through step by step that will allow

  • Users to schedule the backup to be taken on a particular interval
  • Delete the backup copies after a certain period of time

Prerequisites:

  • SQL server 2000/2003/2008/2012
  • SQL server Agent (Up and running state)

Steps: Schedule the database backup Steps:

  1. Make sure that SQL agent is in running state: Go To SQL Server Configuration Manager > SQL Server Services > Run SQL Server Agent (Set it to Run Automatically)
  2. If you are using new versions of SQL server then, Go To SQL Server Management Studio, Find TAB Management > Maintenance Plans. Right Click > Maintenance Plans Wizard.
  3. Text your maintenance plans, ex: DailyBackup. Select Option button : Single Schedule for entire task.
  4. Click Configure > Set Schedule according your demands.
  5. Select Maintenance tasks. for this case, you choose Full Database backup.
  6. Click Next, then define which database to backup, set backup location, and backup extension.
  7. Click Next, Choose your report mode, then Finish.

Code to

DECLARE @FileName varchar(MAX)

SELECT @FileName = (SELECT ‘C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\test1_’ + CONVERT(varchar(20),GETDATE(),112) + REPLACE(CONVERT(varchar(5),GETDATE(),108),’:’,”) + ‘.bak’)
SELECT @FileName

BACKUP DATABASE [test] TO DISK = @FileName WITH RETAINDAYS = 1, NOFORMAT, NOINIT, NAME = N’test-Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10

I have refereed many blogs to automatically delete the old back ups after particular time but no one worked for me. Then I decided to create a power shell script and schedule windows job to run this script. The code for powershell script is as follows:

$backuplimit = (Get-Date).AddMinutes(-15)
$path = “F:\BackUp”

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $backuplimit } | Remove-Item -Force

Hope this helps you to schedule backups of the sql databases