Monday, July 15, 2013

SharePoint 2010 - Adding Folders and Items inside Folder using PowerShell

I came across situation where I need to add the Folders and items within that folders using PowerShell to check threshold problem. I thought adding the items more than the threshold value, PowerShell will be ideal to run on SharePoint System.

Here is the PowerShell Script which creates 30 folders inside the Custom List and 200 items in each Folder.
Only requirement to run this Script is custom list named as “ProjectPhones”.

Note:
  • You can have your own list. Just change the name in script (Highlighted)
  • While running script add your Site Url as parameter where the list is situated
PowerShell Script:

Add-PsSnapin Microsoft.SharePoint.PowerShell


# read site url
$siteUrl=$args[0]

write-host $siteUrl
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
write-host $site
$web = $site.OpenWeb()

write-host $web
$list = $web.Lists["ProjectPhones"]
write-host $list

$F=1
$i=1


for($F=1; $F -le 30; $F++)
{
                write-host "Creating folder - Folder " + $F
                $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
                $folder["Title"] = "Folder" + $F
                $folder.Update();


                for($i=1; $i -le 200; $i++)
                {
                                write-host "Adding " + $i  + " Item in Folder[" + $F + "]"

                                $ListItem = $list.AddItem($folder.Folder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)

                                $ListItem["Title"] = "PhoneNumber Folder-" + $F + "-" + $i
                               

                                $ListItem.Update()
                }
}
$web.Dispose()
$site.Dispose()


Remove-PsSnapin Microsoft.SharePoint.PowerShell

Hope this helps anyone in need.

Tuesday, May 14, 2013

SharePoint 2010 - Web part page with Quick Launch

when you create webpart page, it is always created without quick launch. This is normal behavior of SharePoint. i have seen most of the developer stuck over here to get the quick launch.

Here is the solution to get quick launch on our new pages created using web part pages.

Before we see solution lets understand how and where quick launch is displayed.

SharePoint has master page  and each page is getting inherited from the master page. Quick launch is placed on master page under contentplaceholder id "PlaceHolderLeftNavBar" and Div Tag named as "s4-leftpanel"




Where as in webpart page this contentPlaceHolder is empty as well as the div "s4-leftpanel" which display the quick launch is hidden using script (See Below)




Solution:
  1. Remove hte ContentPlaceHolder with name "PlaceHolderLeftNavBar" from webpart Page
  2. Change the Display: None to Display: Block in CSS Script.

Thursday, February 14, 2013

Access User information from User Profile using SharePoint Object Model

Access User Profile from Console Application to retrieve user information

Most of the time we require user information for our application. the best way to get these information from User Profile. Here is the code which describes how we can connect to User Profile service and get the user information.

I assumed that User Profile service application is already setup by Farm Admin.


I also assumed that you have Web App already created e.g. http://win-urbt9f048vo:9999.

Following is the Code which will access user profile service application to get the information of specific user.

Required Namespace:
  • System.Web
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfile
  • Microsoft.SharePoint

Code 1st Part: Here we are trying to access the Site and pass the User ID to FillUserProfileDetails Function.


Code 2nd Part: Here we are trying to get the service context of the Site. After getting service context creating UserProfileManager Object which will fetch the user data using GetUserProfile method.

GetUserProfile Method require the login id of the user.

Once it return the object UserProfile filled with all information, each property can be individually.
All default properties details can be found here. properties are mapped with AD. yo can also retrieve any custom property created in User Profile.