Thursday, February 2, 2012

Sharepoint Visual Upgrade

                    
To upgrade all sitecollections in a webapplication, use the below script

$webapp = Get-SPWebApplication
http://webappURL
foreach ($s in $webapp.sites)
{$s.VisualUpgradeWebs() }
 

To upgrade all subsites in a sitecollections , use the below script

$site = Get-SPSite
http://siteURL/
 foreach ($web in $site.AllWebs){$web.UIVersion = 4;$web.Update();}

To Check the upgrade status for a site, use the below script.


$sc = Get-SPSite
http://siteurl ; $sc.GetVisualReport()

Sharepoint Discussion list : Retreive items from discussion list programmatically

Here is the sample on retrieving data from sharepoint discussion list programamtically. The below sample retrieves data based on the user name and the date of the post/reply to the post

            string url = "http://siteURL/";   // Your Site url
            string listName = "Team Discussion";//Discussion List name
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb oWebsiteRoot = site.OpenWeb())
                {
                    SPList list = oWebsiteRoot.Lists[listName];
                    SPListItemCollection allPosts = list.Folders;

                    SPQuery qry = new SPQuery();
                    qry.ViewAttributes = "Scope=\"Recursive\"";
                    SPListItemCollection allReplies = list.GetItems(qry);

                    List<SPListItem> allItems = allPosts.OfType<SPListItem>().Union(allReplies.OfType<SPListItem>()).ToList<SPListItem>();

                    string createdBy = "username";
                    string startDt = "1/12/2012";
                    string endDt = "1/14/2012";

                    DateTime dtStart = Convert.ToDateTime(startDt);
                    DateTime dtEnd = Convert.ToDateTime(endDt);

                    IEnumerable<SPListItem> finalList = from n in allItems.OfType<SPListItem>()
                                                      where n["Created By"].ToString().Split('#')[1] == createdBy && (Convert.ToDateTime(n["Created"]) >= dtStart && Convert.ToDateTime(n["Created"]) < dtEnd)
                                                      select n;

                    foreach (SPListItem x in finalList)
                   {
                        Console.WriteLine("Title=" + ((x.Name== "") ? "Reply from "+x["DiscussionTitle"] : x["Title"]));
                    }
                    Console.WriteLine("---Program Completed. Press enter to exit-----");
                    Console.ReadLine();
                }
            }

Sharepoint 2010 : How to map Claim Provider with Token issuer

The main purpose of a custom claims provide is 
1) Name resolution & 
2) Claims augmentation. 

The token issuer (for example ADFS) issues claims and the custom claims provider will provide name resolution and augment for those claims issued by the token issuer. Thus, you need to map both claims provider and token issuer. If we do not map them, then the claims we resolve in the search dialog or type-in control will not map to the account name or other claim that someone logs in with.

Object Model Code

            SPSecurityTokenServiceManager manager = SPSecurityTokenServiceManager.Local;
            SPTrustedLoginProviderCollection providers=manager.TrustedLoginProviders;
          
            foreach (SPTrustedLoginProvider i in providers) //I have looped all the trusted provider
            {
//you can check the name before applying the custom claim provider
                i.ClaimProviderName = "CustomClaimsProviderName";
                i.Update();
            }
            manager.Update();



Powershell Script

$trustedProv = Get-SPTrustedIdentityTokenIssuer -Identity "Trusted Login Provider Name"
$trustedProv.ClaimProviderName = “Custom Claims Provider Name”  
$trustedProv.Update()