Thursday, September 29, 2011

Migrate SharePoint List Data to Document Library without changing the Created and Modified Date


 SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite("http://br-pc-203/"))
                {
                    SPList customList = site.RootWeb.Lists["Custom List"];
                    SPList documentLibrary = site.RootWeb.Lists["Document Library"];
                    SPFolder folder = site.RootWeb.Folders[documentLibrary.Title];
                    SPFileCollection fileCollection = null;
                    foreach (SPListItem ltItem in customList.Items)
                    {
                        if (ltItem.Attachments != null && ltItem.Attachments.Count > 0)
                        {
                            foreach (String sFilename in ltItem.Attachments)
                            {
                                fileCollection = folder.Files;
                                SPListItem docItem = documentLibrary.Items.Add();
                                SPFile attachmentFile =  ltItem.ParentList.ParentWeb.GetFile(ltItem.Attachments.UrlPrefix + sFilename);

                                string destFile = fileCollection.Folder.Url + "/" + attachmentFile.Name;
                                byte[] fileData = attachmentFile.OpenBinary();

                                SPFile addedFile = fileCollection.Add(destFile, fileData, site.RootWeb.CurrentUser, site.RootWeb.CurrentUser, Convert.ToDateTime(ltItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(ltItem[SPBuiltInFieldId.Modified]));
                                SPListItem item = addedFile.Item;
                                item[SPBuiltInFieldId.Created] = Convert.ToDateTime(ltItem[SPBuiltInFieldId.Created]);
                                item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(ltItem[SPBuiltInFieldId.Modified]);
                                addedFile.Item.Update();
                            }
 
                        }
                    }
                }
            });

Sunday, September 18, 2011

OOB Redirect Page for Display and Edit form of ListItem : COPYUTIL.ASPX provided by SharePoint


To navigate to a SharePoint list item or document, when you only got the ID's of the item, the item's container (list or document library) and the item's web? I sure did! For example when you query SharePoint data by making use of the SPSiteDataQuery class; the resulting data table includes all those ID's (and additional properties if you want), but it doesn't include a link back to the item. You could make use of the Object Model to build the link in code, but that is both resource intensive and pretty complex. In that case you have to check out the CopyUtil.aspx page, which is also used by the Content Query Web Part by the way. The CopyUtil.aspx page is an application page to which you can provide a bunch of ID's, as a result the page will redirect you to the corresponding item or document.

You just have to build a URL like this (replace the X's with the actual ID's of course):


http://yoursite/_layouts/CopyUtil.aspx?Use=id&Action=dispform&ItemId=X&ListId=X&WebId=X&SiteId=X

And the CopyUtil.aspx page will do the rest!

Action = "dispform" or Action = "editform"

Saturday, September 17, 2011

List filter with URL values SharePoin


Did you know that you easily can filter your Sharepoint list or library with values in your URL? This means that you can create a “view” without really creating a view. To spice up your GUI you can for instance insert some graphical boxes above your document library view that filters with URL values. Last, but not least, you can use this method to filter the content of all web parts on a page, you are not limited to filter a view in a document library (after all, a view is just a web part on a .aspx page). You can filter on a built-in column, or a column you have created yourself.
Example:
Here I have created a couple of links in the Qiuck Launch. The url for the first link could be:
/Forms/AllItems.aspx?FilterField1=_ModerationStatus&FilterValue1=2
I used a built in column (you will have to use the internal column name) in this example, if you use your own column you just use your columns initial name and the plain value. The filter sign next to the column name shows that you have made a regular filtering of the column. Ofcourse you can filter on multiple values and columns, I guess you can figure that out for yourself:)

How do I know that “2″ equals “pending”? Apply the filter manually and check out the URL, you’ll se both the internal column name and the correct value to filter on.
In other words, adding the following code to your URL makes your URL-filtering day in Sharepoint:
?FilterField1=ColumnName&FilterValue1=ValueToFilterOn

Saturday, September 10, 2011

Reset the Farm Passphrase in SharePoint 2010


Adding a new server to the farm and you can’t remember that new SharePoint 2010 addition called the Farm Passphrase? No problem! You can use PowerShell to reset / change that forgotten farm passphrase. Note that I said reset and not retrieve. There is currently no option to retrieve the existing farm passphrase.
  • Must be a member of the SharePoint_Shell_Access database role on the SharePoint Configuration database. Check Get-SPShellAdmin and Add-SPShellAdmin.
  • Open the SharePoint 2010 Management Shell (Start > All Programs > Microsoft SharePoint 2010 Products > SharePoint 2010 Management Shell)
  • Enter this at the PowerShell prompt:
    • $passphrase = ConvertTo-SecureString -asPlainText -Force
  • Input the new passphrase and hit Enter
  • Enter this at the PowerShell prompt:
    • Set-SPPassPhrase -PassPhrase $passphrase -Confirm
  • You will be asked to confirm the passphrase by re-entering it
  • Re-enter the passphrase and hit Enter
  • You will be asked if you are sure that you want to perform this action, type Y (for Yes) and hit Enter
  • Your farm passphrase has now been reset!