The Microsoft.SharePoint.Deployment object model is designed to work with data ranging from an entire Web site to a single item in a list or library. You can select the level of metadata to include with migrated content, as well as choose whether to do a full migration or provide only an incremental update.
Consider an example where you have a list with 1000 items. You have exported list items in the list using the SPExport API.
Sample code for Exporting Listitems is provided below
string strUrl = "http://<servername>";
//Export code
SPSite site = new SPSite(strUrl);
SPList list = site.RootWeb.Lists["<ListName>"];
if (list.Items.Count > 0)
{
SPExportSettings expSetting = new SPExportSettings();
expSetting.ExportMethod = SPExportMethodType.ExportAll;
expSetting.FileCompression = false;
expSetting.FileLocation = @"F:\temp\";
SPListItem item = list.Items[0];
expSetting.BaseFileName = item.ID.ToString() + ".cmp";
expSetting.LogFilePath = @"F:\temp\Export.log";
expSetting.OverwriteExistingDataFile = true;
expSetting.HaltOnNonfatalError = false;
expSetting.HaltOnWarning = false;
expSetting.IncludeSecurity = SPIncludeSecurity.All;
expSetting.IncludeVersions = SPIncludeVersions.LastMajor;
expSetting.SiteUrl = strUrl;
SPExport exp = new SPExport(expSetting);
SPExportObject obj = new SPExportObject();
obj.Id = item.UniqueId;
obj.ParentId = (item.File == null) ? item.ParentList.ID : item.File.ParentFolder.UniqueId;
obj.Type = (item.File == null) ? SPDeploymentObjectType.ListItem : SPDeploymentObjectType.File;
obj.IncludeDescendants = SPIncludeDescendants.All;
obj.ExcludeChildren = false;
exp.Settings.ExportObjects.Add(obj);
exp.Run();
}
If you have deleted the list by mistake and you would want to retain the list items using the Export details available with you. You can use SPImport API to import the list items, however you will have make changes to some of the xml files which are created by the Export script given above.
Steps to be followed
-
Create a calendar List and add an item to it. For example name the list as TestImport.
-
Export the List using the Export.txt given above.
-
Make a note of the GUID of the list and the ParentFolderId(RootFolder) of the list.
-
Delete the list and create a new Calendar list using by the same name (For Ex:TestImport).
-
Make a note of the GUID of the newly created list and ParentFolderId(RootFolder) of the list.
-
Replace the old GUID of the list with new GUID in the xml files (Manifest.xml, RootObjectMap.xml and ExportSettings.xml).
-
Replace the old ParentFolderId of the list with new ParentFolderId in the Manifest.xml file.
-
Change the ContentType Id by adding 2 Hexadecimal value to the existing ContentTypeId (For Ex: add FF at the end of thecontentType Id), save the file. You will have to change this in 2 places in Manifest.xml file.
-
Run the Import script given below
Import Script
===============
SPImportSettings importSettings = new SPImportSettings();
string strUrl = "http://<ServerName>";
importSettings.CommandLineVerbose = true;
importSettings.RetainObjectIdentity = true;
importSettings.FileCompression = false;
importSettings.FileLocation = @"F:\temp\";
importSettings.SiteUrl = strUrl;
importSettings.IncludeSecurity = SPIncludeSecurity.None;
importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
importSettings.UpdateVersions = SPUpdateVersions.Ignore;
importSettings.LogFilePath = @"F:\temp\Import.log";
importSettings.Validate();
SPImport import = new SPImport(importSettings);
import.Run();
Calling BreakRoleInheritance(false)on a List, internally flips the AllowUnsafeUpdates flag to false, and this may lead to below mentioned error, if we do not explicitly set the AllowUnsafeUpdates to true again.
When any object that implements ISecurable (those are SPWeb, SPList and SPListItem) breaks or reverts their role definition inheritance. This means every time you call
SPRoleDefinitionCollection.BreakInheritance(), BreakRoleInheritance(), ResetRoleInheritance() or set the value of HasUniquePerm the AllowUnsafeUpdates
property of the parent web will reset to its default value and you may need to set it back to true in order to do further updates to the same objects.
Therefore, it can be safely concluded that , if we intend to do SPWeb and SPSite modifications after a call to BreakRoleInheritance for a List, a recommended usage
is to couple it with an explicit setting of AllowUnsafeUpdates property to true for the web & site again.
Attaching Sample code
---------------------------------------
SPSite objSPSite = new SPSite("<ServerURL>");
SPWeb objSPWeb = objSPSite.OpenWeb(objSPSite.OpenWeb().ID);
SPListCollection lists = objSPWeb.Lists;
Guid docLibGuid = lists.Add("Doc Lib 1", "Doc Lib Desc", SPListTemplateType.DocumentLibrary);
SPList docLib = lists[docLibGuid];
docLib.EnableVersioning = true;
docLib.OnQuickLaunch = true;
docLib.Update();
objSPWeb.AllowUnsafeUpdates = true;
docLib.BreakRoleInheritance(false); //Exception is thrown in this line of code.
docLib.Update();
When we remove docLib.BreakRoleInheritance(false) everything works fine.
Will have to change the last two lines of code as below
docLib.BreakRoleInheritance(false); //Exception is thrown in this line of code.
objSPWeb.AllowUnsafeUpdates = true;
docLib.Update();
While trying to query custom managed property using FullText Search getting an error
"Property does not exist or it is used in a manner inconsistent with the schema settings".
We would not be able to query Custom managed property using Microsoft.Sharepoint.Search.
We will have to use Microsoft.Office.Server.Search.
You could refer to this MSDN article
http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.fulltextsqlquery.aspx
Add the following register tag in your aspx page
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Add the sharepoint "FormDigest" control in the page.
<SharePoint:FormDigest ID="fd" runat="server"/>
Add the following code to add the user profile into sharepoint group:
SPSite site = null;
ServerContext server = null;
UserProfileManager upm = null
site = new SPSite("<http://servername/ssp/admin>");
server = ServerContext.GetContext(site);
upm = new UserProfileManager(server, false);
string sUser = "<User1>";
string sDomain = "Domain1";
////create user sample
string sAccount = sDomain + "\\" + sUser;
if (!upm.UserExists(sAccount))
{
UserProfile u = upm.CreateUserProfile(sAccount);
Response.Write("User created successfully"
}
There is a restriction on number of characters (255 characters) allowed for a Multi line text field added to a document library when you try to input more than 255 characters through UI.
Attaching a sample code which will help in working around the problem of restriction on number of characters (255 characters) allowed for a Multi line text field added to a document library.
SPSite mySite = new SPSite("<http://SiteURL>");
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["<DOCLIBNAME>"];
SPField myField = myList.Fields["MULTILINE-FIELDNAME"];
((SPFieldMultiLineText)myField).UnlimitedLengthInDocumentLibrary = true;
myField.Update();
Had a scenario where in I had to get User Identity of one of my colleague in My Profile page of MySite in MOSS.
Sample Code snippet
------------------------------------
SPSecurity.RunWithElevatedPrivileges(delegate()
{
IPersonalPage page = this.Page as IPersonalPage;
using (SPSite mysitenew = new SPSite(page.PersonalSite.Url))
{
writer.Write("Real User: " + mysitenew.Owner.ToString());
}
});
Check if you have a dll called Security.dll in your bin folder of MOSS.
Why does it fail?
We have Security.dll which comes with OS and it is required for authentication purposes between server and client.
When we have Security.dll in bin folder of MOSS, IIS Loads this dll and assumes this as the dll which is required for Authentication. As it cannot find the appropriate method and properties the authentication fails and the error is thrown
Added a new custom field to a document library, wanted to update the metadata of
the custom field with something like Name = Anthony using Front Page RPC method
Was not able to do it, tried with
1. '\='
2.'%3D'
3. '='
The Value to be used is %5c%3d
For setting the metadata value to Name = Anthony -- Name %5c%3d Anthony.
Code sample to set the metadata information of a custom field using Front page
RPC
public void SetMetaData(string uri)
{
Uri myUri = new Uri(uri); // URL of the doc like
http://<servername>/sites/<siteName>/Shared Documents/<document name>.doc
string webUrl // sites/<Site Name>
string fileUrl; // Shared Documents/<document Name>
UrlToWebUrl(uri, out webUrl, out fileUrl);
string postBody = String.Format("method=set+document+meta-info&service_name=" +
webUrl + "&document_name={0}&meta_info=[Hello;SR|Test+'='+Dat]", fileUrl);
SendRequest(myUri.GetLeftPart(UriPartial.Authority) + webUrl +
"/_vti_bin/_vti_aut/author.dll", postBody);
}
Code snippet to move documents across sites with metadata along with version history using SPS object Model and Front Page RPC
NOTE : It updates all the metadata apart from Modified By and Modified date
Attachment(s): MoveDocumentsWithVersion.zip