Tuesday, May 22, 2012

Restore a deleted site collection in SharePoint Server 2010 powershell script


Restore a deleted site collection in SharePoint Server 2010

In prior versions of SharePoint Server 2010, if a site collection (that is, an SPSite object) was accidentally deleted, there was not a direct way of restoring a specific deleted site collection. The only method to restore a deleted site collection was to restore the entire farm from a backup. This was costly and time consuming, and typically was not performed.
When a site collection is accidentally deleted in SharePoint Server 2010 with SP1, the deleted site collection is stored in the SPDeletedSite object, not the SPSiteobject. Therefore, to restore a deleted site collection, you must use the Restore-SPDeletedSite Windows PowerShell cmdlet or programmatically access the object model.

PowerShell script

This will restore all the deleted site the same location, if site already exists it wont, use -force to restore


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null){

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

}


foreach($deletedSite in Get-SPDeletedSite)
{
Write-Host $ deletedSite  ;
Restore-SPDeletedSite -Identity $deletedSite 
}

happy Coding ,
Power of PowerShell

Saturday, May 19, 2012

upload attachments to SharePoint, replacing them with links in outlook 2010

Hi friends,
as many are asking me to how can we achieve this “"upload attachments to SharePoint, replacing them with links in outlook 2010””
Here is my blog for those need help regarding the same. This I developed long back probably two years back.
how to achieve this
Through client object model, and you have template for the same in VS2010 to create all the links for the outlook
use client object model, create a new section for SharePoint in outlook panel, provide a buttons so that people can add there own document libraries, while adding check whether they have access and add it, store them in local outlook directory of the user,
when user right click on any document, there also you need to have one more links when they hover, you have to show them all the configured document libraries [Dynamically you need to read the local file and generate xml format], when they click recursively show all the folders in the document library so that they can select the folder where they want to save it, click save, it moves the document and replaced with the link,
Ribbons:
Top of the outlook need a ribbon so that it can be configured for all the sharepoint document link stuffs
when u add outlook add in you will get a ribbon.xml there you can have your own ribbon and for click event you need to write the code of to do something good
how to read attachment from the outlook mail item
there is two type of classes OUTLOOK.Inspector and outlook.explorer
the item within the out look will be inspector and when open a email in a single window, then it is a child to the outlook so there also you need to have same functionality
   1: public void OnMyButtonClick(Office.IRibbonControl control)
   2:        {
   3:            List<SharePointSites> Sitelist = new AddList().LoadXMLData();
   4:            SharePointSites foundsite = Sitelist.Find(delegate(SharePointSites s) { return s.FullPath == control.Tag; });
   5:            string msg = string.Empty;
   6:  
   7:            if (control.Context is Outlook.Inspector)
   8:            {
   9:                msg = "Context=Inspector" + "\n";
  10:                Outlook.Inspector insp =
  11:                    control.Context as Outlook.Inspector;
  12:                if (insp.AttachmentSelection.Count >= 1)
  13:                {
  14:                    Outlook.AttachmentSelection attachSel =
  15:                        insp.AttachmentSelection;
  16:                    foreach (Outlook.Attachment attach in attachSel)
  17:                    {
  18:                        Form1 form = new Form1(attach, foundsite.SiteUrl, foundsite.SiteLibray);
  19:                        form.Show();
  20:                    }
  21:                }
  22:                else
  23:                {
  24:                    OutlookItem olItem =
  25:                        new OutlookItem(insp.CurrentItem);
  26:                    msg = msg + olItem.Subject;
  27:                }
  28:            }
  29:            else if (control.Context is Outlook.Explorer)
  30:            {
  31:                msg = "Context=Explorer" + "\n";
  32:                Outlook.Explorer explorer =
  33:                    control.Context as Outlook.Explorer;
  34:                if (explorer.AttachmentSelection.Count >= 1)
  35:                {
  36:                    Outlook.AttachmentSelection attachSel =
  37:                        explorer.AttachmentSelection;
  38:                    foreach (Outlook.Attachment attach in attachSel)
  39:                    {
  40:                        Form1 form = new Form1(attach, foundsite.SiteUrl, foundsite.SiteLibray);
  41:                        form.Show();
  42:                    }
  43:                }
  44:                else
  45:                {
  46:                    Outlook.Selection selection =
  47:                        explorer.Selection;
  48:                    if (selection.Count == 1)
  49:                    {
  50:                        OutlookItem olItem =
  51:                            new OutlookItem(selection[1]);
  52:                        msg = msg + olItem.Subject
  53:                            + "\n" + olItem.LastModificationTime;
  54:                    }
  55:                    else
  56:                    {
  57:                        msg = msg + "Multiple Selection Count="
  58:                            + selection.Count;
  59:                    }
  60:                }
  61:            }
  62:            else if (control.Context is Outlook.AttachmentSelection)
  63:            {
  64:                msg = "Context=AttachmentSelection" + "\n";
  65:                Outlook.AttachmentSelection attachSel =
  66:                    control.Context as Outlook.AttachmentSelection;
  67:                foreach (Outlook.Attachment attach in attachSel)
  68:                {
  69:                    Form1 form = new Form1(attach, foundsite.SiteUrl, foundsite.SiteLibray);
  70:                    form.Show();
  71:                }
  72:  
  73:            }
  74:  
  75:        }







in the form i will load all the folders in the document library to which user wants to move the file, so that they can create a folder as well in library and move it thier files.


   1: private void btnSave_Click(object sender, EventArgs e)
   2:        {
   3:  
   4:            string library = treeView1.SelectedNode.FullPath;
   5:            this.Close();
   6:            attachment.SaveAsFile(Application.StartupPath + "\\" + attachment.FileName);
   7:            Stream fStream = System.IO.File.OpenRead(Application.StartupPath + "\\" + attachment.FileName);
   8:            byte[] contents = new byte[fStream.Length];
   9:            fStream.Read(contents, 0, (int)fStream.Length);
  10:            fStream.Close();
  11:            System.IO.File.Delete(Application.StartupPath + "\\" + attachment.FileName);
  12:  
  13:  
  14:            //populate information about the new file
  15:            FileCreationInformation fci = new FileCreationInformation();
  16:            fci.Url = attachment.FileName;
  17:            fci.Content = contents;
  18:            fci.Overwrite = true;
  19:            
  20:         
  21:            try
  22:            {
  23:                //get context of our sharepoint
  24:                ClientContext ctx = new ClientContext(siteURL);
  25:                ctx.AuthenticationMode = ClientAuthenticationMode.Default;
  26:                //get the site colleciton
  27:                Web web = ctx.Web;
  28:                ctx.Load(web);
  29:                ctx.Load(ctx.Web.Lists);
  30:                ctx.ExecuteQuery();
  31:                //get the document library folder
  32:                Folder docSetFolder = web.GetFolderByServerRelativeUrl(library);
  33:                ctx.ExecuteQuery();
  34:                //load the file collection for the documents in the library
  35:                FileCollection documentFiles = docSetFolder.Files;
  36:                ctx.Load(documentFiles);
  37:                ctx.ExecuteQuery();
  38:  
  39:                //add this file to the file collection
  40:                Microsoft.SharePoint.Client.File newFile = documentFiles.Add(fci);
  41:                newFile.ListItemAllFields["Title"] = fci.Url;
  42:                newFile.ListItemAllFields.Update();
  43:                ctx.Load(newFile, ff => ff.ListItemAllFields["Title"], ff => ff.ServerRelativeUrl);
  44:                ctx.ExecuteQuery();
  45:  
  46:                Outlook.MailItem current = (Outlook.MailItem)attachment.Parent;
  47:                Uri FileUrl = new Uri(siteURL + newFile.ServerRelativeUrl);
  48:                current.Body = string.Format("Your file is replaced with following link\n----------------------\n" + FileUrl.AbsoluteUri + "\n--------------\n" + current.Body);
  49:              
  50:                attachment.Delete();
  51:                //Form1_Load(sender, e);
  52:            }
  53:            catch (Exception ex)
  54:            {
  55:                this.Close();
  56:                MessageBox.Show(ex.Message);
  57:            }
  58:        }

for ribbon,


   1: <?xml version="1.0" encoding="UTF-8"?>
   2: <customUI  xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
   3:   <ribbon>
   4:     <tabs>
   5:       <tab id="MyTab" visible="true"
   6:            label="Share Point">
   7:         <group label="MyGroup" id="MyGroup" >
   8:           <button id="AddList"
   9:                   size="large"
  10:                   label="Add lists"
  11:                   screentip="Adds SharePoint lists to outlook"
  12:                   supertip="Adds SharePoint lists to outlook"
  13:                   onAction="OnAddListClick"/>
  14:         </group>
  15:       </tab>
  16:     </tabs>
  17:   </ribbon>
  18:  
  19:  
  20:   <contextMenus>
  21:     <contextMenu idMso="ContextMenuAttachments">
  22:       <dynamicMenu label="Save to SharePoint" visible="true" id ="MyDynamicMenu" getContent="getContent" invalidateContentOnDrop="true" />
  23:     </contextMenu>
  24:   </contextMenus>
  25:  
  26: </customUI>
First xml menu is for SharePoint document libray links.. so users can have there own multiple document libraries so that they can send thier attachments to any document libraries which they want.
imageimage
 
2nd xml context menu.. when you right click on the attachments it gives a another menu to move it to sharepoint.
image
imageimage
note: getContent will pull the xml menus dynamically and create xml menus to move it to all the document libraries which previously configured
   1: public string getContent(Office.IRibbonControl control)
   2:         {
   3:  
   4:             StringBuilder SitesXml = new StringBuilder();
   5:             if (System.IO.File.Exists(Application.UserAppDataPath + "\\" + AddList.SitesFile))
   6:             {
   7:                 AddList addlist = new AddList();
   8:                 List<SharePointSites> ListOfSites = addlist.LoadXMLData();
   9:                 SitesXml = new StringBuilder(@"<menu xmlns=""http://schemas.microsoft.com/office/2009/07/customui"" >");
  10:  
  11:                 int i = 1;
  12:                 foreach (SharePointSites item in ListOfSites)
  13:                 {
  14:                     string buttonId = "button" + i++;
  15:                     SitesXml.Append(@"<button id=""" + buttonId + @""" label=""" + item.SiteLibray + @""" tag=""" + item.FullPath + @""" onAction=""OnAction""  imageMso=""SignatureLineInsert""/>");
  16:                     SitesXml.Append(@"<menuSeparator getTitle=""GetTitle"" />");
  17:  
  18:                 }
  19:                 SitesXml.Append(@"</menu>");
  20:             }
  21:             return SitesXml.ToString();
  22:         }



Source - http://msdn.microsoft.com/en-us/library/ee692172.aspx
http://blogs.msdn.com/b/jensenh/archive/2006/04/06/569876.aspx 
 
outlook
http://www.dotnetspider.com/forum/163233-how-read-attachments-from-outlook-o-hard-disk.aspx
 
VSTO Packaging,
The add in needs to give it as a package, for this VSTO frame work is required to publish the addi in. sometimes client may not have the necessary environment to install the add in, in that time your package needs to get those download from the internet so that use can download the necessary framework and VSTO to carry out the installation,
as we know we are using the client object model it needs to two dll on the client machine that also needs to be packaged properly, SharePoint.client and SharePoint.client.Runtime.
image
if you have any doubts please free to ask me