Showing posts with label sharepoint 2010. Show all posts
Showing posts with label sharepoint 2010. Show all posts

Sunday, 4 December 2016

PowerShell - how to Read write csv file


Recently Some one has asked me about Reading and writing text file of csv file using powershell, its pretty simple and streght forward, but I think I should write it down that will help beginners.

Below is the code, let's have overview before jumping in to the code.

First We are loading SharePoint dll, it is require if you are performing any action related to SharePoint, if not, please remove those lines (line number 5 to 18)

Than we are checking if file exist of not,

If file exist we will laod the file using Import-CSV cmdlets

and than itarateusinf forwach loop to read all the rows.
You can read current rows specified columns value like $row."ColumnName"



1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#Created by Praveen to import items in SharePoitn list 
#uploaded 18728 items in minutes 4.5 minutes
#Code is free to use, but don't forget to put Author's name

# Setup the correct modules for SharePoint Manipulation
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
   Add-PsSnapin Microsoft.SharePoint.PowerShell
}
$host.Runspace.ThreadOptions = "ReuseThread"

#Open SharePoint List

 $SPAppList="MyListName"
 $spWeb = Get-SPWeb "http://ServerSiteUrl/sites/ABC"
 $spData = $spWeb.Lists[$SPAppList]
 $InvFile="C:\Data_T9078.csv" 

 # Get Data from Inventory CSV File
 $FileExists = (Test-Path $InvFile -PathType Leaf)
 if ($FileExists) {
    "Loading $InvFile for processing..."
    $tblData = Import-CSV $InvFile
 } else {
    "$InvFile not found - stopping import!"
    exit
 }

 # Loop through Applications add each one to SharePoint

 "Uploading data to SharePoint...."
 $count=1;
 foreach ($row in $tblData)
 {
    "$count) Adding entry for "+$row."ColumName1".ToString()
    $spItem = $spData.AddItem()
    $spItem["Title"] = $row."ColumName1".ToString()
    $spItem["Project"] = $row."Project".ToString()
    $spItem["Category"] = $row."Category".ToString()
    $spItem["Details"] = $row."Details".ToString()
    $spItem.Update()
    ++$count;
 }

 write-host "---------------"
 write-host "Upload Complete"

 $spWeb.Dispose()

CSV file format



You can also Export data to the CSV by refering below line of code.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$csvFile = "C:\DataExport_Pra09098.csv" 

#Array to Hold Result - PSObjects
$ListItemCollection = @()
"Starting export...."
$count=1; 

 #Get All List items where task date is before 2014
 $list.Items |  Where-Object { $_["Created"] -le "12/31/2014"} | foreach {
 $ExportItem = New-Object PSObject 
 $ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_["Title"]
 $ExportItem | Add-Member -MemberType NoteProperty -name "Project" -value $_["Project"]
 $ExportItem | Add-Member -MemberType NoteProperty -Name "Category" -value $_["Category"]
 $ExportItem | Add-Member -MemberType NoteProperty -name "Author" -value $_["Author"]
 $ExportItem | Add-Member -MemberType NoteProperty -name "Editor" -value $_["Editor"]
 $ExportItem | Add-Member -MemberType NoteProperty -name "Created" -value $_["Created"]
 $ExportItem | Add-Member -MemberType NoteProperty -name "Modified" -value $_["Modified"]
 $ListItemCollection += $ExportItem
 write-host "$count) Getting Item ID "$_["ID"] ;++$count;
 } 
 #Export the result Array to CSV file
 $ListItemCollection | Export-CSV $csvFile -NoTypeInformation                       


--
Comments & Feedback are welcome, and yeah don't forget to share this make short link.
Praveen

Sunday, 16 October 2016

Automated Windows Server monitoring, maintance on daily alert email using powershell, check low space

Hey, Hello Dear reader, we have another blog, that is useful for Windows Admin, Server Admin or SharePoint Admins also,

We have number of servers for productions, UAT and Integration so going to each server (DB, WFE, Application and DMZ servers) etc and checking for frees pace or Server should have all running services running etc that's really hectic and time consuming, people already started making thier task automated to monitor 10 of or even 100s of server..

In short, yeah we can MAKE WORK Short as blog link says, we can do it in simpler way.

we need to create a task scheduler and run PowerShell script that can check for free space, running services and process etc... and send an email for all these details.

Here I am sharing sample script to check timely Server space daily and sends an alert email using powershell.





Any time above sample can be downloaded on clicking Download here link.

--
Comments & Feedback are welcome, and yeah don't forget to share this make short link.



Tuesday, 19 July 2016

SharePoint accordion with list items, FAQ sample

SharePoint online, Office 365, SharePoint 2013, SharePoint 2010 accordion option / menus, needed depending of how and what data you have to show to the users, here I am writing an example of FAQs section with Accordion as below example is using only jQuery, Css and SPServies to show data from SharePoint list to your page, this sample can be added in App for learning purpose.

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <TITLE>SharePoint Accordion option using jQuery and css : sample</TITLE>
    <style type='text/css'>
        #AccordionByPraveen {
            list-style: none;
            padding: 0px;
            width: 99%;
            margin: auto;
        }
        #AccordionByPraveen li {
            display: block;
            background-color: #0074C3;
            color: white;
            font-size: 15px;
            cursor: pointer;
            padding: 5px 5px 5px 8px;
            list-style: circle;
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px;
            border-radius: 3px;
        }
        #AccordionByPraveen ul {
            list-style: none;
            padding: 7px;
            display: none;
            border: 1px silver solid;
        }
        #AccordionByPraveen ul li {
            font-weight: normal;
            font-size: 15px;
            font-family: 'segoe ui';
            cursor: auto;
            color: black;
            background-color: #fff;
            padding: 5px 5px 5px 12px;
        }
        #AccordionByPraveen a {
            text-decoration: none;
        }
        #AccordionByPraveen .faqHeadings {
            border-top: 1px white solid;
            padding: 15px;
            font-family: 'Arial Rounded MT Bold';
            font-family: 'Verdana';
        }
        #AccordionByPraveen .faqHeadings:hover {
            background-color: #338DCD!important;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(e) {

            $("#AccordionByPraveen > li").live('click', function() {
                $("#AccordionByPraveen > li").css('background-color', '#0074C3');
                $(this).css('background-color', '#00487B');

                if (false == $(this).next().is(':visible')) {
                    $('#AccordionByPraveen > ul').slideUp(400);
                }
                $(this).next().slideToggle(400);
            });
            AddFAQs();
            $('#AccordionByPraveen > ul:eq(0)').show();
            // $('#AccordionByPraveen > ul:eq(0)').parent().css('background-color','#00487B');
        });

        function AddFAQs() {
            var faqListName = "FAQs";
            var camlViewFields = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Details' /></ViewFields>";
            var htmlLis = "";
            var countLi = 1;
            $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: faqListName,
                CAMLViewFields: camlViewFields,
                completefunc: function(xData, Status) {
                    //alert(Status+faqListName );
                    $(xData.responseXML).SPFilterNode("z:row").each(function() {
                        var ttl = $(this).attr("ows_Title");
                        var ttlDetails = $(this).attr("ows_Details");
                        ttl = countLi + ") " + ttl;
                        htmlLis += '<li class="faqHeadings">' + ttl + '</li><ul>';
                        htmlLis += '<li>' + ttlDetails + '</li></ul>';
                        ++countLi;
                    }); // end completefunc            
                }
            });
            if (htmlLis.indexOf("faqHeadings") > 0) {
                //alert(htmlLis);
                $("#AccordionByPraveen").append(htmlLis);
            } else {
                htmlLis = "<li class='faqHeadings'>FAQ couldn't loaded. Please inform Admin or raise a help ticket with SharePoint team in AskAlexion.</li><ul><li>Details not found.</li></ul>";
                $("#AccordionByPraveen").append(htmlLis);
            }
        }
    </script>
</head>
<body>
    <H2 class="headingFAQPage">Frequently Asked Questions</H2>
    <ul id="AccordionByPraveen">
        <li class="faqHeadings">How can start form to initnin I can nto see sumbit button</li>
        <ul>
            <li>Accordion same data for first li. you can do this and this to open the form</li>
        </ul>
        <li class="faqHeadings">I am new to the site how can I setup my profile </li>
        <ul>
            <li>Contact you website admins or create Help ticket, this is another reply.</li>
        </ul>
        <li class="faqHeadings">Latest feature and upcoming event details can be found at?</li>
        <ul>
            <li>This is the url so site ,http.... and see option number three etc etc.</li>
        </ul>
    </ul>
Original post link is http://makeshortwork.blogspot.in/ 
</body>
</html>


Below is working demo, it may be its not looking perfect as some of the google css are applied here forcefully. 

SharePoint Accordion option using jQuery and css : sample

Frequently Asked Questions

  • How can start form to initnin I can nto see sumbit button
    • Accordion same data for first li. you can do this and this to open the form
  • I am new to the site how can I setup my profile
    • Contact you website admins or create Help ticket, this is another reply.
  • Latest feature and upcoming event details can be found at?
    • This is the url so site ,http.... and see option number three etc etc.