Changing the name of a sharepoint list programmaticaly - c#

I'm trying to change the "name" of a sharepoint document library (not the title). I have found a way to do it from sharepoint designer: http://www.thesharepointblog.net/Lists/Posts/Post.aspx?List=815f255a-d0ef-4258-be2a-28487dc9975c&ID=52 What I need is to do this programmaticaly. How do I do that?

"Name" is an actual root folder of the list. So you need to rename it
using (SPSite mySite = new SPSite("http://server"))
{
//open the relevant site
using (SPWeb myWeb = mySite.OpenWeb("TestSite"))
{
//loop through the lists
foreach (SPList list in myWeb.Lists)
{
//when we find the correct list, change the name
if (list.RootFolder.Name == "OrigListName")
{
//move the root folder
list.RootFolder.MoveTo(list.RootFolder.Url.Replace("OrigListName", "OrigListName_New"));
}
}
}
}

Related

Sharepoint online CSOM generate list item display url

I have a file list inside desktop app thats fetched from the sharepoint online document library. What i want to do is to provide a posibility to show that file in the browser. But i'm not able to generate proper url.
Here is a csom code snippet to get file url in a document library:
using (ClientContext ctx = new ClientContext("https://zheguo.sharepoint.com/sites/dev/"))
{
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List targetList = ctx.Web.Lists.GetByTitle("Documents");
ListItemCollection ItemCol = targetList.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(ItemCol);
ctx.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in ItemCol)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
Console.WriteLine(new Uri(ctx.Url).GetLeftPart(UriPartial.Authority) + item["FileRef"]);
}
}
}
Reference:
Getting the Absolute URL of a File in CSOM

Sharepoint 2013 "Everyone" user permission

I have Sharepoint enterprise server with list that have "Everyone" user grant as contributor
but, when I execute below code with authenticated user (let say, user Abc), for add item list row, the row never added to list
SPWeb web1 = SPContext.Current.Web;
using (SPSite site = new SPSite(web1.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList roomList = web.Lists.TryGetList(LIST_NAME);
if (roomList != null)
{
SPListItem newItem = roomList.Items.Add();
PopulateListWithData(data, ref newItem);
newItem.Update();
}
}
}
}
How can I add row to list with user Abc?
try to add using following code
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web))
{
SPList roomList = web.Lists.TryGetList(LIST_NAME);
if (roomList != null)
{
SPListItem newItem = roomList.AddItem();
PopulateListWithData(data, ref newItem);
newItem.Update();
}
}
}
if you still have the same issue
try to ensure the following
list you try to add to already exists in root web site
List name is types right because your code may be break after TryGetList
after that if still have the same issue you need to debug or check sharepoint logs to catch exception that happen

Sharepoint access to list ignore user permission

I created web part (something like wizard) and need change item value in list, but when get list item, they haven't items (logged user haven't access to this list). Can I ignore sharepoint permission, and update this value?
I use LINQ to sharepoint and get context:
using (SystemOcenContextDataContext ctx = new SystemOcenContextDataContext("http://sh2010/sites/270"))
{
// code :)
}
Update:
make test when get list using:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedSite = new SPSite("http://sh2010/sites/270"))
{
using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb())
{
list = ElevatedWeb.Lists["Ankiety i oceny"];
}
}
});
the object list "have" items
but in my project I use sharepoint linq datacontext when using:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SystemOcenContextDataContext ctx = new SystemOcenContextDataContext("http://sh2010/sites/270"))
{
item = ctx.AnkietyIOceny.First();
}
});
the context(ctx) didn't have any items :/
any idea?
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Pur your code here.
});
Get more details Here
The SharePoint linq provides doesn't work with ElevatedPrivileges. It accesses the SPWeb.Current instance which will have the access rights of the request and not the elevated user.
http://jcapka.blogspot.com/2010/05/making-linq-to-sharepoint-work-for.html
There's a work around, which I've implemented generally the same thing. It's a big awkward but it works as far as I can tell.

List properties are not updating

I have an Image List on each and every web (SPWeb) of a SiteCollection. I want to set a specific property of this List. I am iterating through all the Sites withing a SiteCollection and finding the List and setting its properties. My problem is that I can set the properties of a List present at first level Sites, but can't set the properties of Lists, present at 2nd or 3rd level Sites. For example,
Here is site hierarchy:
Home (Rootweb) 1st level
Home-> Aboutus (subsite) 2nd level
Home->Aboutus->Our Mission (subsite) 3rd level
here is the code for that!
using (SPSite oSPsite = new SPSite(http://spdev/))
{
foreach (SPWeb web in oSPsite.AllWebs)
{
SPList list = web.GetList("PublishingImages");
if (list != null)
{
foreach (SPContentType contentType in list.ContentTypes)
{
if (contentType.Name == "Publishing Picture")// but id is better
{
list.EnableModeration = false;
list.Update();
}
}
}
web.Dispose();
}
}
Is it because I'm disposing the parent first?
Assuming the list name is the same on every site (PublishingImages) and you're on WSS 3.0 or MOSS07 here is the sample code:
using (SPSite oSPsite = new SPSite("yourSiteUrlHere"))
{
SPWebCollection siteWebs = oSPsite.AllWebs;
foreach (SPWeb web in siteWebs)
{
try
{
SPList list = null;
try
{
list = web.Lists["PublishingImages"];
}
catch {}
if (list != null)
{
// todo: update list properties here
list.Update();
}
}
finally
{
if(web != null)
web.Dispose();
}
}
}
As Ashutosh mentioned, there are some properties that don't work on all list types but I'm assuming since you've already stated it works on some of them you aren't setting any of those.

How to get list of Folders and subfolders created in "List"?

I am trying to access list of all Sites and Lists from Sharepoint 2007 using c#.
I am able to get Name of sites and list.
But unable to get folders and subfolders of particular list.
And Document uploaded in particular Folder.
I am using Web Services (no dependency of Microsoft.Sharepoint.dll)
Regards,
Jene
Try this:
using(SPSite site = new SPSite("http://yoursite"))
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["your_doclib"];
SPQuery query = new SPQuery()
{
Query = "",
ViewAttributes = #"Scope=""RecursiveAll"""
};
SPListItemCollection itens = list.GetItems(query);
foreach (SPListItem item in itens)
{
Console.ForegroundColor =
item.FileSystemObjectType == SPFileSystemObjectType.Folder ?
ConsoleColor.White : ConsoleColor.Gray;
Console.WriteLine("{0}", item.Name);
}
}

Categories