Working with folders in SharePoint (using Powershell)

$web = Get-SPWeb http://len-shareappt
$subFolderName = “D” #Create first subfolder in doc library “C” named “D”
$numberFoldersToCreate = 2;
$list = $web.Lists.TryGetList(‘C’) #Create first doc library named “C”

# Get handle on the subfolder
$subFolder = $list.RootFolder.SubFolders.Item($subFolderName);

#Create folders within an existing folder in a document library

# Create desired number of subfolders
for($i=1; $i -le $numberFoldersToCreate; $i++)
{
$folder = $list.AddItem($subFolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, “$folderNamePrefix$i”)
$folder.Update()
write-host $i
}

#Create folders in the root of a document library

# Create desired number of subfolders
for($i=1; $i -le $numberFoldersToCreate; $i++)
{
$folder = $list.AddItem(“”, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, “$folderNamePrefix$i”)
$folder.Update()
write-host $i
}

Working with folders in SharePoint (using Powershell)

How to Restrict Permissions on List Views? (Audience targeting)

1. Create view A, B, C foe list TestList

2. Create user group X, Y, Z

3. Open the TestList, bu default all items will display in AllItems.aspx page

4. Edit the page by using Site Action

5. Click on the Modify Shared Web Part link of TestList . Now select the View A from selected dropdown

6. Go to Advanced section and select user group in the target audience. Apply the changes

7. Add the TestList web part again to the same AllItems.aspx page.

7. Repeat the step 5 and 6 for other two views and set the target audience respectively.

8. Now the AllItems.aspx page will have three web part for one list i.e TestList
Credits http://social.technet.microsoft.com/Forums/sharepoint/en-US/3730d47f-6ba2-4ffd-ac05-567b042ff5c1/how-to-restrict-permissions-on-list-views

 

How to Restrict Permissions on List Views? (Audience targeting)

EnsureUser vs Web.User

web.EnsureUser()

эта хрень добавляет пользователя
и прописывает везде куда нужно

юзера взять по логину

web.user[login] \ web.siteuser[login]

тока надо еще всё это в трай кетч обернуть, если юзера нема в коллекции получишь ексепшен

 

 

Have you tried to open context as an user which profile do you want to retrieve?

SPUser user = web.EnsureUser(@"domain\name");
SPUserToken token = user.UserToken;
using (SPSite site = new SPSite(SPContext.Current.Site.ID, token ))
{

}

In this case try something like this

public class ContextSwitcher: IDisposable
{
           private HttpContext _temp;
           private SPSite _site;
           private SPWeb _web;

           public ContextSwitcher(string url, string username)
          {
                      SPUser user = web.EnsureUser(username);
                      SPUserToken token = user.UserToken;

                      _site = new SPSite(url, token);
                      _web = _site.OpenWeb();
                      _temp = HttpContext.Current; 

                     HttpRequest request = new HttpRequest("", _web.Url, "");
                     request.Browser = new HttpBrowserCapabilities();
                     HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
                     HttpContext.Current.Items["HttpHandlerSPWeb"] = _web;
          }

          public void Dispose()
         {
                     HttpContext.Current = _temp;
                     if (_web != null) _web.Dispose();
                     if (_site != null) _site.Dispose();
          }

###############

Finally I figured it out. UserProfileManager internally checks HttpContext.Current.User to determine if current user can view property value. The trick is to replace context and user (if HttpContext.Current.User is null, null reference thrown in UserProfileManager constructor, so this is required). The final code looks like this one:

SPSecurity.RunWithElevatedPrivileges(delegate
{
    var context = HttpContext.Current;
    HttpContext.Current = new HttpContext(new HttpRequest(string.Empty, SPContext.Current.Site.Url, string.Empty), new HttpResponse(new StringWriter()));
    HttpContext.Current.User = new GenericPrincipal(WindowsIdentity.GetCurrent(), new string[0]);
    var manager = new UserProfileManager(SPServiceContext.GetContext(HttpContext.Current));
    var profile = manager.GetUserProfile(@"domain\name");
    var value = profile["my_property"].Value;
    HttpContext.Current = context;
});  

RunWithElevatedPrivileges required because only under elevated code we can guarantee that WindowsIdentity.GetCurrent() returns application pool account. And this account should have sufficient permissions for user profile application (to read private properties).
It works, but I only doubt if this is the best and safe solution.

EnsureUser vs Web.User