Posted on

Windows AD Manager Code Snippet

 

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.DirectoryServices.AccountManagement;
  5. using System.DirectoryServices.ActiveDirectory;
  6.  
  7. namespace PB.ADUserManagement.Library
  8. {
  9.     public class ADManager
  10.     {
  11.         private ContextType _Context;
  12.  
  13.         public ADManager(ContextType context)
  14.         {
  15.             this._Context = context;
  16.             this.StartUp();
  17.         }
  18.  
  19.         public ADManager()
  20.         {
  21.             this._Context = ContextType.Domain;
  22.             this.StartUp();
  23.         }
  24.  
  25.         public static string CurrentDomain()
  26.         {
  27.             string name;
  28.             try
  29.             {
  30.                 name = Domain.GetCurrentDomain().Name;
  31.             }
  32.             catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
  33.             {
  34.                 name = Environment.MachineName;
  35.             }
  36.             catch (ActiveDirectoryOperationException activeDirectoryOperationException)
  37.             {
  38.                 name = Environment.MachineName;
  39.             }
  40.             return name;
  41.         }
  42.  
  43.         public ReadOnlyCollection<string> Groups()
  44.         {
  45.             PrincipalSearcher principalSearcher = new PrincipalSearcher()
  46.             {
  47.                 QueryFilter = new GroupPrincipal(new PrincipalContext(this._Context))
  48.             };
  49.             PrincipalSearchResult<Principal> principals = principalSearcher.FindAll();
  50.             List<string> strs = new List<string>();
  51.             foreach (Principal principal in principals)
  52.             {
  53.                 strs.Add(principal.SamAccountName);
  54.             }
  55.             strs.Sort();
  56.             return new ReadOnlyCollection<string>(strs);
  57.         }
  58.  
  59.         private static bool IsMemberOfDomain()
  60.         {
  61.             bool flag = false;
  62.             try
  63.             {
  64.                 Domain.GetComputerDomain();
  65.                 flag = true;
  66.             }
  67.             catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
  68.             {
  69.             }
  70.             return flag;
  71.         }
  72.  
  73.         private void StartUp()
  74.         {
  75.             if (!ADManager.IsMemberOfDomain())
  76.             {
  77.                 this._Context = ContextType.Machine;
  78.             }
  79.         }
  80.  
  81.         internal List<Principal> Users(string groupName)
  82.         {
  83.             PrincipalSearcher principalSearcher = new PrincipalSearcher();
  84.             GroupPrincipal groupPrincipal = new GroupPrincipal(new PrincipalContext(this._Context))
  85.             {
  86.                 SamAccountName = groupName
  87.             };
  88.             principalSearcher.QueryFilter = groupPrincipal;
  89.             GroupPrincipal groupPrincipal1 = (GroupPrincipal)principalSearcher.FindOne();
  90.             List<Principal> principals = new List<Principal>();
  91.             principals.AddRange(groupPrincipal1.Members);
  92.             return principals;
  93.         }
  94.     }
  95. }

 

High Level Usage is as follows;

var object = new ADManager();

cmbBox cmbBox.DataSource = object.Groups();

Posted on

MCT Classes Play Ground for MS Server Platform

http://mctclassnotes.blogspot.com/

This is my instructor’s Dan’s Posting in regards to class Activity.

 

Q: Where can I find a SharePoint 2013 “playground”, i.e. a SharePoint farm I can learn on or test things on?

A: This is a question that comes up a lot, and there are no really great answers, but there are some pretty good ones.
Probably your best bet, if you have a system with Hyper-V and adequate resources, is to download this set of VHDs:
https://www.microsoft.com/en-us/download/details.aspx?id=40266
https://www.microsoft.com/en-us/download/details.aspx?id=40267
https://www.microsoft.com/en-us/download/details.aspx?id=40265
It’s actually a “Lync Server 2013 Test Drive” image, but it also includes Exchange 2013 and SharePoint 2013.
Another possibility, for targeted experimentation purposes, is to use the TechNet Virtual Labs:
https://technet.microsoft.com/en-us/virtuallabs/bb467605.aspx
These are labs with specific objectives, e.g. “Introduction to Backup and Restore in Microsoft SharePoint 2013”, but they give you an online instance of SharePoint to do the lab work on. It only gives you a couple of hours to complete the lab, but they do have the merit of being almost instantaneously accessible.
Of course, if you don’t mind paying $5/month, you could always get yourself the bare-bones version of SharePoint online:
https://products.office.com/en-us/SharePoint/compare-sharepoint-plans
For additional options, this guy:
http://www.matthewjbailey.com/sharepoint-2013-dev-environment/
…has compiled a pretty decent list of options.

POSTED BY DAN COSTELLO

Q: How do I programmatically dump the contents of a SharePoint list to an Excel file, ideally on a schedule?

There are a lot of ways of doing that, depending on your specific goals.
For example:
Here’s a guy who’s thrown together a simple little console application using the server-side
object model, you could pretty readily adapt it to a timer job or just run it on a SharePoint server with Windows scheduler.
http://sharepointplays.blogspot.com/2013/11/programmatically-export-data-from.html
Or you could use powershell and dump it as a csv:
https://kmlsp.wordpress.com/2014/08/04/how-to-export-sharepoint-list-to-csv-file-by-powershell/
(By the way, here’s a really nice example of doing it the other way ‘round, i.e. csv file—>SharePoint
list.)
https://blogs.technet.microsoft.com/stuffstevesays/2013/06/06/populating-a-sharepoint-list-using-powershell/
Personally, I’d prefer using an SSIS package. This is the MSDN article about the SharePoint list adapters for SSIS:
https://msdn.microsoft.com/en-us/library/hh368261.aspx?f=255&MSPPError=-2147217396
…and this is a slightly old but still relevant and nicely-documented article about how to do an extract:
http://dataqueen.unlimitedviz.com/2011/06/how-to-use-a-sharepoint-list-as-a-data-source-in-your-ssis-package/

POSTED BY DAN COSTELLO

Q: How do I get SharePoint 2013 Excel Services workbooks to refresh their data automatically?

Microsoft Certified BI consultant Tavis Lovell has written a very good and thorough step-by-step guide to configuring automatic data refreshes:
http://tavislovell.com/the-dark-art-of-the-excel-services-workbook-data-refresh/ .
It’s a longish post, but he covers manual refreshes, refreshing every time the book is opened, and scheduled refreshes.

POSTED BY DAN COSTELLO

Q: Why is my log backup failing periodically?

A: I got this question via email from a student:

So we have this job that backs up the transaction log every hour.
Whenever I shrink the transaction log, this backup will fail until the next full backup of the database. Why?
This is what the SHRINK script looks like:
USE [master]
GO                                                                                                                                 
ALTER DATABASE [DBNAME] SET RECOVERY SIMPLE
GO
USE [DBNAME]
GO                                                                                                                                
DBCC SHRINKFILE (N’DBNAME_log’ , 0, TRUNCATEONLY)
GO                                                                                                                                
USE [master]
GO
ALTER DATABASE [DBNAME] SET RECOVERY FULL WITH NO_WAIT
GO
Is there a particular time to do a shrink?

I’ve found that this is a pretty common issue, so I wanted to address a couple of the problems with this maintenance script.
First, it’s not the shrinking, it’s the “SET RECOVERY SIMPLE”.
https://technet.microsoft.com/en-us/library/jj835093%28v=sql.110%29.aspx
The relevant text from that article is (emphasis added):

The Log Chain
A continuous sequence of log backups is called a log chain. A log chain starts with a full backup of the database. Usually, a new log chain is only started when the database is backed up for the first time or after the recovery model is switched from simple recovery to full or bulk-logged recovery. Unless you choose to overwrite existing backup sets when creating a full database backup, the existing log chain remains intact. With the log chain intact, you can restore your database from any full database backup in the media set, followed by all subsequent log backups up through your recovery point. The recovery point could be the end of the last log backup or a specific recovery point in any of the log backups. For more information, see Transaction Log Backups (SQL Server).
To restore a database up to the point of failure, the log chain must be intact. That is, an unbroken sequence of transaction log backups must  extend up to the point of failure. Where this sequence of log must start depends on the type of data backups you are restoring: database, partial, or file. For a database or partial backup, the sequence of log backups must extend from the end of a database or partial backup. For a set of file backups, the sequence of log backups must extend from the start of a full set of file backups. For more information, see Apply Transaction Log Backups (SQL Server).

However, you shouldn’t be regularly shrinking your log files anyway. You should find a size where it never needs to autogrow and leave it at that size.
http://www.sqlskills.com/blogs/paul/why-you-should-not-shrink-your-data-files/
That article is primarily about shrinking data files, but includes this: “…shrinking the log should be a rare operation and should not be part of any regular maintenance you perform.” Presumably the reason that you’re shrinking the log file regularly is that you’ve got regular autogrowth happening, which is almost guaranteed to introduce fragmentation into the file.
https://support.microsoft.com/en-us/kb/315512
From that link: “Physical fragmentation from changing the size of the data or log files can have a severe affect on your performance. This is true whether you use the automatic settings or whether you manually grow and shrink the files frequently.”
So, in summary:

  • To allow your log backups to run without error, leave the database in full recovery mode.
  • Don’t shrink your log (or data) files as part of regular maintenance.
  • Pre-size your log file, leaving autogrowth turned on as a contingency.

POSTED BY DAN COSTELLO

Q: How do I refresh the bookshelf in the Skillpipe iOS app?

A: The question is, essentially: “I am using the Skillpipe Reader app for iOS on my iPad, and I have added a new book to my online ‘bookshelf’, but it doesn’t show up on the iPad. How do I get it to show up?”
This is obviously a pretty specific question, but someone asked it and I couldn’t find an answer anywhere online, so I’m putting it here in an effort to save someone else a little trouble. Also, honestly, because the answer was so surprising.
I wrote to Arvato, the company that makes Skillpipe. This was their reply (copied and pasted, i.e. all grammar and spelling is verbatim):

“Thank you for your e-mail regarding Skillpipe for iOS.
Please note that our responsible team is currently working on resolving
the issue with the refreshing the Skillpipe App for iOS.
In the mean time as work around please ask the student to re-install the
app.
That will force the app to refresh.
We since apologize for inconvenience.”

Got that? To refresh the contents of the bookshelf, uninstall, and then reinstall, the app. Which means that you will then have to log in again and re-download all of the books you previously had downloaded.
So there you have it.

POSTED BY DAN COSTELLO

Q: How do you keep SSMS icons from being tiny in High-DPI Displays?

OK, this one’s a bit of a cheat — I wasn’t asked this question directly in class. But while working on something else I encountered this problem in my own environment, which is Parallels Desktop on a Mac Powerbook with a retina display, and the solution was so arcane that I just had to share it here in case it helps someone else:
SSMS in High-DPI Displays: How to Stop the Madness – SQLServerCentral
The problem is basically that SSMS doesn’t work well with the operating system when it comes to scaling UI components, and the solution is to instruct it to use an “older” mechanism for drawing the UI. Specifically, it draws the UI in a buffer then scales it to the display and dumps it to the screen…

POSTED BY DAN COSTELLO

Q: How do I make sure I have all of the correct SharePoint 2013 service packs and cumulative updates installed?

The process of patching a newly installed SharePoint 2013 farm is very nicely outlined here:
https://blog.imason.com/fully-patch-sharepoint-2013-with-sp1/
That article is dated, of course, but still relevant, you just have to make sure you get the most recent Cumulative Update:
https://technet.microsoft.com/en-us/library/dn789211%28v=office.14%29.aspx#BKMK_2013

POSTED BY DAN COSTELLO

Posted on

How to develop SharePoint 2013 on the Dev Machine

During the recent research, I came across following article.

https://msdn.microsoft.com/en-us/library/office/ee554869.aspx

 

Install the operating system for your SharePoint Add-ins development environment

The requirements for a development environment for an installation of SharePoint are less stringent and costly than the requirements for a production environment. In any development environment, you should use a computer with an x64-capable CPU, and at least 16 GB of RAM to install and run SharePoint; 24 GB of RAM is preferable. Depending on your specific requirements and budget, you can choose one of the following options:

Install the app development prerequisites for the operating system and SharePoint 2013

SharePoint requires your operating system to have certain prerequisites installed before installation begins. For this reason, SharePoint includes a PrerequisiteInstaller.exe tool that installs all of the prerequisites for you. Run this tool before running the Setup.exe tool.

  1. Run the PrerequisiteInstaller.exe tool.

  2. Run the Setup.exe tool included with your installation files.

  3. Accept the Microsoft Software License Terms.

  4. On the Choose the installation you want page, choose Stand-alone.

    Figure 2. Installation type choice

    SharePoint 2013 Installation Server Type

  5. If any errors occur in the installation, review the log file. To find the log file, open a Command Prompt window, and then type the following commands at the command prompt. A link to the log file also appears when the installation is complete.

    MS-DOS

    cd %temp
    dir /od *.log
    
  6. After the installation is complete, you are prompted to start the SharePoint Products and Technologies Configuration Wizard.

    Note Note

    The SharePoint Products and Technologies Configuration Wizard can fail if you are using a computer that is joined to a domain but that is not connected to a domain controller. If this failure occurs, connect to a domain controller either directly or through a Virtual Private Network (VPN) connection, or sign in with a local account that has administrative privileges on the computer.

  7. After the configuration wizard is complete, you see the Template Selection page of the new SharePoint site.

    Figure 3. Choose site template page

    SharePoint 2013 site templates

Install Visual Studio

When you install Visual Studio, you get all of the templates, tools, and assemblies to develop SharePoint on your local development machine.

See Installing Visual Studio for instructions about installing Visual Studio.

Verbose logging in Visual Studio

Follow these steps if you want to turn on verbose logging:

  1. Open the registry, and navigate toHKEY_CURRENT_USER\Software\Microsoft\VisualStudio\nn.n\SharePointTools, where nn.n is the version of Visual Studio, such as 12.0 or 14.0.

  2. Add a DWORD key named EnableDiagnostics.

  3. Give the key the value 1.

The registry path will change in future versions of Visual Studio.

Next steps

If you will be creating workflows, continue with Set up and configure SharePoint 2013 Workflow Manager

Posted on

Draw Morris Chart with MVC Web

Following is working View Name is: testChart.cshtml;
@{
ViewBag.Title = “testChart”;

}

<h2>testChart</h2>
<!DOCTYPE html>
<html>
<head>
<title> Test Page </title>
</head>
<body>
<div id=”myfirstchart” style=”height: 250px;”></div>
<div id=”Line-Example” style=”height: 250px;”></div>
<div id=”morris-donut-chart” style=”height: 250px;”></div>
</body>
</html>
<script type=”text/javascript”>

$(document).ready(function () {
$.get(‘@Url.Action(“GetSalesGraphData”)’, function (result) {

new Morris.Line({
// ID of the element in which to draw the chart.
element: ‘myfirstchart’,
// Chart data records — each entry in this array corresponds to a point on

data: result,
// The name of the data record attribute that contains x-values.
xkey: ‘Year’,
// A list of names of data record attributes that contain y-values.
ykeys: [‘Sales’],
// Labels for the ykeys — will be displayed when you hover over the
// chart.
labels: [‘Sales’]
});
});

 

})
</script>

@*<link rel=”stylesheet” href=”//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css”>

<script src=”//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js”></script>
<script src=”//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js”></script>*@

<script src=”@Url.Content(“~/bower_components/morrisjs/morris.min.js”)”></script>
<script src=”@Url.Content(“~/Scripts/morris-data.js”)”></script>

 

Following is Controller Page:

Using EF6 entity name: AWorksEntities, Stroed Procedure, Using ViewModel

[HttpGet]
public JsonResult GetSalesGraphData()
{ using (var context = new AWorksEntities())
{

var sales = context.f_uspGetTotalYearlySales();
List<YearlySales> ys = sales.Select(s=> new YearlySales
{
Year = s.Year.GetValueOrDefault(),
Sales = s.TotalSales.GetValueOrDefault()

}).ToList();
return Json(ys, JsonRequestBehavior.AllowGet);

}

}

public ViewResult testChart()
{
return View();
}